George Shuklin
George Shuklin

Reputation: 7877

How to enumerate list in ansible?

I have this structure:

addresses:
 - 192.168.1.1
 - 192.168.2.2
 - 192.168.3.3

I need to process them in the task:

- tasks:
      - name: Iterating
      - template: src=template.j2 dest=/etc/addresses/{{index}}.conf
        with_items: addresses

But I can't find any way to fill index variable (or any other similar trick).

Note: I know about indexes inside j2 templates, but I'm talking about tasks.

Upvotes: 5

Views: 10991

Answers (1)

udondan
udondan

Reputation: 59989

You can use with_indexed_items, another Ansible standard loop.

- tasks:
  - name: Iterating
    template: src=template.j2 dest=/etc/addresses/{{ item.0 }}.conf
    with_indexed_items: addresses

The address item can be accessed in the template as {{ item.1 }}

Upvotes: 11

Related Questions