Create yaml template to create group and passing the users via list in openshift / kubernetes

2/1/2022

I need to add several users to a new group template

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: group-devops-template
objects:
- apiVersion: user.openshift.io/v1
  kind: Group
  metadata:
    name: ${GROUP_NAME}
  users: ["${USER_NAMES}"] # <--- here the issue
  #- user1
  #- user2
  #- user3
parameters:
- name: GROUP_NAME
  description: Password used for Redis authentication

- name: USER_NAMES
  description: user or users to add

The nightmare happens when I process the template with the values.

oc process -f grup_template3 -p GROUP_NAME=admin_devops  -p USER_NAMES=["user1","user2","user3"]  | oc create -f -

And here my questions:

  • How to specify the list correctly in the above template
  • How to specify correctly the list of values: "user1","user2","user3"

The final group with the desired users should like this:

oc get groups admin_devops -o yaml
apiVersion: user.openshift.io/v1
kind: Group
metadata:
  name: admin_devops
users:
- user1
- user2
- user3

Any idea about how to achieve this?

Thank you for the help

-- lordJeA
kubernetes
openshift
yaml

1 Answer

2/1/2022

Your users var should be written as ${{USER_NAMES} and USER_NAMES var should be formatted as a correct yaml. Here is working example:

template.yaml

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: group-devops-template
objects:
- apiVersion: user.openshift.io/v1
  kind: Group
  metadata:
    name: ${GROUP_NAME}
  users: ${{USER_NAMES}}
parameters:
- name: GROUP_NAME
  description: Password used for Redis authentication
- name: USER_NAMES
  description: user or users to add

command:

oc process -f template.yaml -p GROUP_NAME=admin_devops  -p USER_NAMES='["user1","user2","user3"]' --local=true -o yaml

output:

apiVersion: v1
items:
- apiVersion: user.openshift.io/v1
  kind: Group
  metadata:
    name: admin_devops
  users:
  - user1
  - user2
  - user3
kind: List
metadata: {}
-- Andrew
Source: StackOverflow