coopz81
coopz81

Reputation: 35

Ansible loops matching on array/dict_list

I have an array like so

our_domains:
  - domain: www.example.com
    urls:
      - { path: '/' }
      - { path: '/test1' }
  - domain: www.example2.com
    urls:
      - { path: '/' }
      - { path: '/test2' }

and in a template I want to match on a given domain and then loop over the contents of urls

e.g

    {% if item.value.domain == 'www.example2.com' %}
    {% for item_url in item.urls %}

   service_description    http://anotherwebsite.com{{ item_url['path'] }}

    {% endfor %}
    {% endif %}

Im sure the FOR loop will work ok as its working in a similar context elsewhere in the code.

Im just struggling getting a conditional match on the domain name.

Any help would be appreciated

Thanks

Upvotes: 0

Views: 183

Answers (1)

ilias-sp
ilias-sp

Reputation: 6705

it looks like you want to execute the template task for each of the elements of the our_domains list.

you should just remove the value from the if statement, rest looks fine:

{% if item.domain == 'www.example2.com' %}
{% for item_url in item.urls %}

service_description    http://anotherwebsite.com{{ item_url['path'] }}

{% endfor %}
{% endif %}

if on the other hand, your intention was to generate only 1 file, you should use this template (one more for loop added enclosing the previous code):

{% for item in our_domains %}
{% if item.domain == 'www.example2.com' %}
{% for item_url in item.urls %}

service_description    http://anotherwebsite.com{{ item_url['path'] }}

{% endfor %}
{% endif %}
{% endfor %}

Upvotes: 1

Related Questions