我有以下剧本:
---
- 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的映射?
答案 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