Alexander D
Alexander D

Reputation: 101

Populating Jinja2 Template with Ansible Variables?

In my Ansible playbook I am trying to set custom variables and then use those variables to populate a license file that I have in json. My idea is something like this.

   vars:
     customFields:
        unique_key: unique_value
        unique_key1: unique_value1

I would then populate the license.json using the template module and jinja2 in this way:

{
    "customer": "{{ customer_name }}",
    "validTil": "{{ lic_valid_till }}",
    {% for field in customFields %}
    "customFields": {
        "{{ field.key }}":"{{ field.value }}"
    }
    {% endfor %}
}

Am I doing this the right way and is something like this supported?

Upvotes: 1

Views: 305

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68394

Yes. It's supported. Use items()

    {% for key, value in customFields.items() %}
    "customFields": {
        "{{ key }}":"{{ value }}"
    }
    {% endfor %}

Upvotes: 1

Related Questions