Search code examples
ansiblejinja2satellite

concatenate variables in Jinja2


I'm struggling with the following example data from Satellite server when templating with Jinja2 in Ansible:

"results": {
    "test.example.com": {
        "interfaces": "eth0,lo",
        "ipaddress_eth0": "10.251.0.45",
        "ipaddress_lo": "127.0.0.1",
        "netmask_eth0": "255.255.255.0",
        "netmask_lo": "255.0.0.0",
        "network_eth0": "10.251.0.0",
        "network_lo": "127.0.0.0",

This piece of code will return: 127.0.0.1, but I want to replace 'ipaddress_lo' with the variable interface. I've tried a million combinations... without any luck... please advice.

{% for items in my_host_facts.json.results %}
{% for interface in my_host_facts.json.results[items].interfaces.split(",") %}
   {{ interface }}:
        address: "{{ my_host_facts.json.results[items].ipaddress_lo }}"
        netmask: "{{ my_subnet.json.mask }}"
        network: "{{ my_subnet.json.network }}"
        gateway: "{{ my_subnet.json.gateway }}"
{% endfor %}
    dns: [ "{{ my_subnet.json.dns_primary }}", "{{ my_subnet.json.dns_secondary }}" ]
{% endfor %}

Kind regards,


Solution

  • Try {{ my_host_facts.json.results[items]['ipaddress_'+interface] }}.