【问题标题】:Is it possible to loop into two different lists in the same playbook (Ansible)?是否可以在同一个剧本(Ansible)中循环进入两个不同的列表?
【发布时间】:2020-08-19 21:17:50
【问题描述】:

我正在编写一个 Playbook Ansible,我想循环进入两个不同的列表。 我现在可以使用with_items 在列表中循环,但是我可以在同一个剧本中使用with_items 两次吗?

这是我想做的:

- 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

所以对于第一次通话,我想使用带有fabric[0]attachs[0] 的剧本。 对于第二个电话,我想使用带有fabric[1]attachs[1] 的剧本。 等等……

有人可以帮帮我吗?

【问题讨论】:

    标签: list loops ansible


    【解决方案1】:

    您希望实现的目标是 with_together,即现在 recommanded 使用 zip 过滤器实现的目标。

    所以:loop: "{{ attachs | zip(fabric) | list }}"

    第一个列表 (attachs) 的元素是 item.0,第二个列表 (fabric) 的元素是 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2012-07-12
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多