Vida Eninkio
Vida Eninkio

Reputation: 91

Is it possible to loop into two different lists in the same playbook (Ansible)?

I'm writing a Playbook Ansible and I want to loop into two different lists. I now that I can use with_items to loop in a list, but can I use with_items twice in the same playbook?

Here is what I want to do:

- name: Deploy the network in fabric 1 and fabric 2
  tags: [merged]
  role_network:
    config:
    - net_name: "{{ networkName }}"
      vrf_name: "{{ vrf }}"
      net_id: 30010
      net_template: "{{ networkTemplate }}"
      net_extension_template: "{{ networkExtensionTemplate }}"
      vlan_id: "{{ vlan }}"
      gw_ip_subnet: "{{ gw }}"
      attach: "{{ item }}"
      deploy: false
    fabric: "{{ item }}"
    state: merged
  with_items:
  - "{{ attachs }}"
    "{{ fabric }}"
  register: networks

So for the first call, I want to use the playbook with fabric[0] and attachs[0]. For the second call, I want to use the playbook with fabric[1] and attachs[1]. And so on...

Can someone help me please?

Upvotes: 2

Views: 540

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39264

What you are looking to achieve is what was with_together and that is, now, recommanded to achieve with the zip filter.

So: loop: "{{ attachs | zip(fabric) | list }}".

Where the element of the first list (attachs) would be item.0 and the element of the second list (fabric) would be item.1.

- name: Deploy the network in fabric 1 and fabric 2
  tags: [merged]
  role_network:
    config:
    - net_name: "{{ networkName }}"
      vrf_name: "{{ vrf }}"
      net_id: 30010
      net_template: "{{ networkTemplate }}"
      net_extension_template: "{{ networkExtensionTemplate }}"
      vlan_id: "{{ vlan }}"
      gw_ip_subnet: "{{ gw }}"
      attach: "{{ item.0 }}"
      deploy: false
    fabric: "{{ item.1 }}"
    state: merged
  loop: "{{ attachs | zip(fabric) | list }}"
  register: networks

Upvotes: 2

Related Questions