内部循环遍历Ansible中的字典

时间:2019-04-08 14:23:52

标签: ansible

在此示例中如何遍历字典? 我可以遍历字符串列表(lorem :),但不能遍历对象:字典。

我正在使用需要作为输入列表和字典的模块,因此我可以使用两种情况,但是在模块调用之前,我必须对字典中的键进行一些变通,因此我必须能够引用它。

我不确定自己在做什么错。谁能给我示范一个合适的例子?

谢谢

---
- name: test
  hosts: localhost
  connection: local

  vars:
    persons:
      foo:
        name: john
        state: us
        objects:
          phone: samsung
          color: black
          capacity: 32
        lorem:
          - 1
          - 2
          - 3
      bar:
        name: helmut
        state: de
        objects:
          phone: lg
          color: red
          capacity: 16
        lorem:
          - 4
          - 5
          - 6

  tasks:

    - name: List of strings is OK
      debug:
        msg: "{{ item.0.value.name }} and object: {{ item.1 }}"
      loop: "{{ persons | dict2items |subelements('value.lorem',{ 'skip_missing': True }) }}"

    - name: Dict referencing key:value is not OK
      debug:
        msg: "Name: {{ item.0.value.name }} and object: {{ item.1.[value] }} with key name: {{ item.1.[key]}}"
      loop: "{{ persons | dict2items |subelements('value.objects',{ 'skip_missing': True }) }}"

产生错误:fatal: [localhost]: FAILED! => {"msg": "the key 'objects' should point to a list, got {u'color': u'black', u'phone': u'samsung', u'capacity': 32}"}

2 个答案:

答案 0 :(得分:1)

我能想到的最好是做这样的事情。使用jinja生成可返回所需内容的列表。

---
- name: test
  hosts: localhost
  connection: local

  vars:
    persons:
      foo:
        name: john
        state: us
        objects:
          phone: samsung
          color: black
          capacity: 32
        lorem:
          - 1
          - 2
          - 3
      bar:
        name: helmut
        state: de
        objects:
          phone: lg
          color: red
          capacity: 16
        lorem:
          - 4
          - 5
          - 6

  tasks:

  - debug:
      msg: |
        [
        {% for p in persons %}
        {% for o in persons[p].objects %}
        {
        "name": "{{ persons[p].name }}",
        "key": "{{ o }}",
        "value": "{{ persons[p].objects[o] }}"
        },
        {% endfor %}
        {% endfor %}
        ]

输出

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        {
            "key": "color", 
            "name": "john", 
            "value": "black"
        }, 
        {
            "key": "phone", 
            "name": "john", 
            "value": "samsung"
        }, 
        {
            "key": "capacity", 
            "name": "john", 
            "value": "32"
        }, 
        {
            "key": "color", 
            "name": "helmut", 
            "value": "red"
        }, 
        {
            "key": "phone", 
            "name": "helmut", 
            "value": "lg"
        }, 
        {
            "key": "capacity", 
            "name": "helmut", 
            "value": "16"
        }
    ]
}

哦,如果您想循环使用它,就像这样。

  - debug:
      msg: "{{ item.name }} {{ item.key }} {{ item.value }}"
    loop_control:
      label: "{{ item.name }} {{ item.key }} {{ item.value }}"
    loop: |
      [
      {% for p in persons %}
      {% for o in persons[p].objects %}
      {
      "name": "{{ persons[p].name }}",
      "key": "{{ o }}",
      "value": "{{ persons[p].objects[o] }}"
      },
      {% endfor %}
      {% endfor %}
      ]

输出

TASK [debug] *******************************************************************
ok: [localhost] => (item=john color black) => {
    "msg": "john color black"
}
ok: [localhost] => (item=john phone samsung) => {
    "msg": "john phone samsung"
}
ok: [localhost] => (item=john capacity 32) => {
    "msg": "john capacity 32"
}
ok: [localhost] => (item=helmut color red) => {
    "msg": "helmut color red"
}
ok: [localhost] => (item=helmut phone lg) => {
    "msg": "helmut phone lg"
}
ok: [localhost] => (item=helmut capacity 16) => {
    "msg": "helmut capacity 16"
}

答案 1 :(得分:0)

您也可以使用with_items。

请参考以下代码:

剧本->

 ---
 - name: test   hosts: localhost   gather_facts: no   tasks:
     - include_vars: vars.yml
     - name: debug
       debug:
         msg: "{{ item.vars.persons }}"
       with_items:
         - "{{ vars }}"

输出--->

    "msg": {
        "bar": {
            "lorem": [
                4,
                5,
                6
            ],
            "name": "helmut",
            "objects": {
                "capacity": 16,
                "color": "red",
                "phone": "lg"
            },
            "state": "de"
        },
        "foo": {
            "lorem": [
                1,
                2,
                3
            ],
            "name": "john",
            "objects": {
                "capacity": 32,
                "color": "black",
                "phone": "samsung"
            },
            "state": "us"
        }
    }
}