Ansible - 使用combine()和with_nested返回一对一映射

时间:2017-09-16 00:26:21

标签: ansible

我有以下剧本:

---
- name: painters
  hosts: localhost
  gather_facts: no
  vars:
    result: {}
  tasks:

    - name: combine painters
      set_fact:
        result: "{{result | combine( { item[0]: item[1] } ) }}"
      with_nested:
        - ['pablo', 'bob']
        - ['picasso', 'ross']

    - name: print grouping
      debug:
        msg: "{{ result }}"

我希望它返回:

ok: [localhost] => {
    "msg": {
        "bob": "ross",
        "pablo": "picasso"
    }
}

但它又回来了:

ok: [localhost] => {
    "msg": {
        "bob": "ross",
        "pablo": "ross"
    }
}

如何让combine命令和with_nested的操作为两位画家输出1:1的映射?

1 个答案:

答案 0 :(得分:2)

我想你想要with_together。如果我有:

---
- name: painters
  hosts: localhost
  gather_facts: no
  vars:
    result: {}
  tasks:
    - name: combine painters
      set_fact:
        result: "{{result | combine( { item[0]: item[1] } ) }}"
      with_together:
        - ['pablo', 'bob']
        - ['picasso', 'ross']

    - name: print grouping
      debug:
        msg: "{{ result }}"

我明白了:

播放[画家] ***************************************** *********************

TASK [combine painters] ********************************************************
ok: [localhost] => (item=[u'bob', u'ross'])
ok: [localhost] => (item=[u'pablo', u'picasso'])

TASK [print grouping] **********************************************************
ok: [localhost] => {
    "msg": {
        "bob": "ross", 
        "pablo": "picasso"
    }
}

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