Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Global counters not incrementing in k8s_openssl.cnf.j2 template #2

@igoratencompass

Description

@igoratencompass

Hi,

The k8s_openssl.cnf.j2 template was not working for me, the global counters were not incrementing in the inner loops thus rendering something like this:

[alt_names]
  DNS.1 = kubernetes
  DNS.2 = kubernetes.default
  DNS.3 = kubernetes.default.svc
  DNS.4 = kubernetes.default.svc.cluster.local
  DNS.5 = k9s-api.virtual.local
  DNS.5 = k9s01.virtual.local
  DNS.5 = k9s02.virtual.local
  DNS.7 = k9s03.virtual.local
  IP.1 = 10.3.0.1
  IP.1 = 192.168.0.154
  IP.2 = 192.168.0.1
...
[req]
...

The following modified template using Namespaces with Jinja2 v2.10 renders the file correctly:

{% for item in k8s_openssl_config|sort %}
[{{ item }}]
{% if k8s_openssl_config[item] %}
{% for k in k8s_openssl_config[item]|sort %}
{% if k == 'DNS' %}
{% set ns = namespace(count=0) %}
{% for dns in k8s_openssl_config[item][k] %}
  {{ k }}.{{ loop.index0 }} = {{ dns }}
{% set ns.count = ns.count + 1 %}
{% endfor %}
{% for host in groups['k8s-masters'] %}
  {{ k }}.{{ ns.count }} = {{ host }}
{% set ns.count = ns.count + 1 %}
{% endfor %}
{% elif k == 'IP' %}
{% set ns = namespace(count=0) %}
{% for ip in k8s_openssl_config[item][k] %}
  {{ k }}.{{ loop.index0 }} = {{ ip }}
{% set ns.count = ns.count + 1 %}
{% endfor %}
{% for ip in groups['k8s-masters']|map('extract', hostvars, ['priv_ip'])|list|sort %}
  {{ k }}.{{ ns.count }} = {{ ip }}
{% set ns.count = ns.count + 1 %}
{% endfor %}
{% else %}
  {{ k }} = {{ k8s_openssl_config[item][k] }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions