OpenShift - Create a Group using Ansible

by
Jeremy Canfield |
Updated: June 01 2025
| OpenShift articles
Let's say you have a YAML file named group.yml that contains the following markup. In this example, a group named "my-group" will be created.
apiVersion: user.openshift.io/v1
kind: Group
metadata:
name: my-group
users:
- jone.doe
- jane.doe
Here is how you can create the group using kubernetes.core.k8s.
---
- hosts: localhost
module_defaults:
community.okd.okd:
host: https://api.op.example.com:6443
validate_certs: False
tasks:
- block:
- name: login
community.okd.openshift_auth:
username: john.doe
password: itsasecret
register: openshift_auth_results
- name: apply group.yml
kubernetes.core.k8s:
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
state: present
src: /path/to/group.yml
always:
- name: logout
community.okd.openshift_auth:
state: absent
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
...
Instead of using a YAML file you can define the YAML structure in the kubernetes.core.k8s task.
- name: apply group.yml
kubernetes.core.k8s:
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
state: present
definition:
apiVersion: user.openshift.io/v1
kind: Group
metadata:
name: my-group
users:
- jone.doe
- jane.doe
Here is how you can create the group using community.okd.k8s.
---
- hosts: localhost
module_defaults:
community.okd.okd:
host: https://api.op.example.com:6443
validate_certs: False
tasks:
- block:
- name: login
community.okd.openshift_auth:
username: john.doe
password: itsasecret
register: openshift_auth_results
- name: apply group.yml
community.okd.k8s:
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
state: present
definition: "{{ lookup('template', '/path/to/group.yml') | from_yaml }}"
always:
- name: logout
community.okd.openshift_auth:
state: absent
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
...
Did you find this article helpful?
If so, consider buying me a coffee over at