Problem with Django template, can't unload lower level values ​in YAML template configuration

I'm using kapitan to build yaml configuration for applications. In my template file:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dev
  namespace: {{ inventory.parameters.namespace }}
subjects:
{% for groups in inventory.parameters.rbac.groups %}
  - kind: User
    name: {{ groups }}
    namespace: {{ inventory.parameters.namespace }}
{% endfor %}
roleRef:
  kind: Role
  name: dev
  apiGroup: rbac.authorization.k8s.io

in the variable file:

groups:
  beta:
    - test_user1
    - test_user2
  demo:
    - test_user3
    - test_user4

I get the following output after compilation:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dev
  namespace: m-beta
subjects:
  - kind: User
    name: beta
    namespace: m-beta
  - kind: User
    name: demo
    namespace: m-beta
roleRef:
  kind: Role
  name: dev
  apiGroup: rbac.authorization.k8s.io

How can I change this line:

name: {{ groups }}

so that instead of name I get accounts: test_user1, test_user2, test_user3, test_user4, and not group names?

And the output was:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dev
  namespace: mvideo-beta
subjects:
  - kind: User
    name: test_user1
    namespace: m-beta
  - kind: User
    name: test_user2
    namespace: m-beta
  - kind: User
    name: test_user3
    namespace: m-beta
  - kind: User
    name: test_user4
    namespace: m-beta
roleRef:
  kind: Role
  name: dev
  apiGroup: rbac.authorization.k8s.io

Thank you in advance!

The problem is that you are using the groups variable instead of the elements of your user lists.
Instead, you need to iterate through the users within each group.

A nested loop is required.

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dev
  namespace: {{ inventory.parameters.namespace }}
subjects:
{% for group, users in inventory.parameters.rbac.groups.items() %}
  {% for user in users %}
  - kind: User
    name: {{ user }}
    namespace: {{ inventory.parameters.namespace }}
  {% endfor %}
{% endfor %}
roleRef:
  kind: Role
  name: dev
  apiGroup: rbac.authorization.k8s.io

After a while, your question and my answer will be translated into Russian.

Вернуться на верх