Ansbile:合并嵌套列表/字典结构

Ansbile:合并嵌套列表/字典结构

我想将一个简单的列表/数组合并到一个嵌套的字典中(如果其中一个术语是错误的,请纠正我)

剧本

---
- hosts: localhost
  gather_facts: no
  vars:
    - simple:
      - right
      - middle
      - top
    - nested:
      - hostname: foo
        path:
          - left
      - hostname: bar
        path:
          - blue
          - green
          - yellow
          - "{{ simple }}"

  tasks:
    - name: content of nested
      debug:
        msg: "{{ nested }}"

实际上输出是:

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

TASK [content of nested] *******************************************************
Wednesday 31 August 2016  08:55:13 +0200 (0:00:00.025)       0:00:00.025 ****** 
ok: [localhost] => {
    "msg": [
        {
            "hostname": "foo", 
            "path": [
                "left"
            ]
        }, 
        {
            "hostname": "bar", 
            "path": [
                "blue", 
                "green", 
                "yellow", 
                [
                    "right", 
                    "middle", 
                    "top"
                ]
            ]
        }
    ]
}

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

我想喜欢有是(输出被操纵)

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

TASK [content of nested] *******************************************************
Wednesday 31 August 2016  08:55:13 +0200 (0:00:00.025)       0:00:00.025 ****** 
ok: [localhost] => {
    "msg": [
        {
            "hostname": "foo", 
            "path": [
                "left"
            ]
        }, 
        {
            "hostname": "bar", 
            "path": [
                "blue", 
                "green", 
                "yellow", 
                "right", 
                "middle", 
                "top"
            ]
        }
    ]
}

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

感谢您的想法!

答案1

这个例子中最简单的事情可能是(我不确定以这种方式定义变量背后的想法):

---
- hosts: localhost
  gather_facts: no
  vars:
    - simple1:
      - blue
      - green
      - yellow
    - simple2:
      - right
      - middle
      - top
    - nested:
      - hostname: foo
        path:
          - left
      - hostname: bar
        path: "{{ simple1 + simple 2 }}"

  tasks:
    - name: content of nested
      debug:
        msg: "{{ nested }}"

相关内容