Ansible 内环在环在内环在内环上

Ansible 内环在环在内环在内环上,ansible,Ansible,在这个例子中,如何循环dict? 我可以在lorem:字符串列表上循环,但不能在objects:dict上循环 我使用的模块需要作为输入列表和dict,所以我可以使用这两种情况,但在模块调用之前,我必须使用dict中的键进行一些变通,所以我必须能够引用它 我不确定我做错了什么。谁能给我举个恰当的例子吗 谢谢 --- - name: test hosts: localhost connection: local vars: persons: foo:

在这个例子中,如何循环dict? 我可以在lorem:字符串列表上循环,但不能在objects:dict上循环

我使用的模块需要作为输入列表和dict,所以我可以使用这两种情况,但在模块调用之前,我必须使用dict中的键进行一些变通,所以我必须能够引用它

我不确定我做错了什么。谁能给我举个恰当的例子吗

谢谢

---
- 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 }) }}"

产生错误:致命:[localhost]:失败!=>{msg:key'objects'应该指向一个列表,得到{u'color':u'black',u'phone':u'samsung',u'capacity':32}

您也可以与\u项一起使用

请参阅以下代码:

剧本->

 ---
 - 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"
        }
    }
}

我能想到的最好办法就是这样做。使用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"
        }
    ]
}
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"
}
哦,如果你想在循环中使用它,就像这样

  - 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] => {
    "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"
        }
    ]
}
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"
}
我使用的模块需要输入列表和dict-请详细说明。通过更多的上下文可能更容易理解您需要什么。