O.K.
O.K.

Reputation: 95

How can I pass a specific variable to each iteration in Ansible loops?

For the following task:

- name: include my task
  include_tasks: change_state.yaml
  loop:
    - "{{ var1 }}"
    - "{{ var2 }}"
  loop_control:
    loop_var: switch

I want to pass an additional variable for each iteration, like for the first iteration (var1) --> interface: "{{interface1}}" and for second iteration (var2) --> interface: "{{ interface2 }}" Is there a way to achieve this in Ansible?

Upvotes: 0

Views: 428

Answers (1)

phanaz
phanaz

Reputation: 1524

You can make a dict out of your list item:

- name: include my task
  include_tasks: change_state.yaml
  loop:
    - var: "{{ var1 }}"
      interface: "{{ interface1 }}"
    - var: "{{ var2 }}"
      interface: "{{ interface2 }}"
  loop_control:
    loop_var: switch

The access in your change_state.yaml is then done via switch.var and switch.interface

Upvotes: 1

Related Questions