如何遍历Ansible中的嵌套列表?

时间:2019-05-11 00:35:36

标签: ansible

我有一个设备列表,每个设备都有一个必须在设备上一次创建的不同属性列表。有没有一种方法可以构建嵌套的一组循环来执行此操作? with_nested构造将每个属性应用于每个设备。每个呼叫每个设备只需要一个属性。

这本剧本演示了我尝试过的方法(Ansible 2.7.1,Python 2.7.1):

- name:                                 Test nested list traversal
  hosts:                                localhost
  connection:                           local
  vars:
    Stuff:
      - Name:         DeviceA
        Info:         AInfo
        Values:
        -             ValueA1
        -             ValueA2
      - Name:         DeviceB
        Info:         BInfo
        Values:
        -             ValueB1
        -             ValueB2
        -             ValueB3

  tasks:
  - name: Loop test
    debug:  msg="{{ item.Name }},{{ item.Info }}, {{ item.Values }}"
    with_items: 
      - "{{ Stuff }}"
What I get: (One call per device, containing all attributes at once)

ok: [localhost] => (item={u'Info': u'AInfo', u'Values': [u'ValueA1', u'ValueA2'], u'Name': u'DeviceA'}) =>
  msg: DeviceA,AInfo, [u'ValueA1', u'ValueA2']
ok: [localhost] => (item={u'Info': u'BInfo', u'Values': [u'ValueB1', u'ValueB2', u'ValueB3'], u'Name': u'DeviceB'}) =>
  msg: DeviceB,BInfo, [u'ValueB1', u'ValueB2', u'ValueB3']

What I want (each msg represents a separate operation to be performed 
on the device with just that one attribute)

msg: A, AInfo, ValueA1
msg: A, AInfo, ValueA2
msg: B, BInfo, ValueB1
msg: B, BInfo, ValueB2
msg: B, BInfo, ValueB3

1 个答案:

答案 0 :(得分:2)

您可以使用subelements过滤器来获取所需的内容:

---
- hosts: localhost
  gather_facts: false
  vars:
    Stuff:
      - Name: DeviceA
        Info: AInfo
        Values:
          - ValueA1
          - ValueA2
      - Name: DeviceB
        Info: BInfo
        Values:
          - ValueB1
          - ValueB2
          - ValueB3
  tasks:
    - debug:
        msg: "{{ item.0.Name }}, {{ item.0.Info }}, {{ item.1 }}"
      loop: "{{ Stuff|subelements('Values') }}"
      loop_control:
        label: "{{ item.0.Name }}"

运行上述剧本会让您:

PLAY [localhost] ******************************************************************************************************************************************************************************

TASK [debug] **********************************************************************************************************************************************************************************
ok: [localhost] => (item=DeviceA) => {
    "msg": "DeviceA, AInfo, ValueA1"
}
ok: [localhost] => (item=DeviceA) => {
    "msg": "DeviceA, AInfo, ValueA2"
}
ok: [localhost] => (item=DeviceB) => {
    "msg": "DeviceB, BInfo, ValueB1"
}
ok: [localhost] => (item=DeviceB) => {
    "msg": "DeviceB, BInfo, ValueB2"
}
ok: [localhost] => (item=DeviceB) => {
    "msg": "DeviceB, BInfo, ValueB3"
}

PLAY RECAP ************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0