[英]Ansible set_fact dictionary of lists
我有這個代碼,我試圖將兩個列表組合成一個字典,但沒有運氣。 我是 ansible 的新手。
- hosts: localhost
vars:
service_status: {}
location:
tasks:
- name:
command: echo {{ item }}
register: excecute
with_items:
- hello
- world
- name: Setting facts
set_fact:
service_status: "{{service_status | combine({ 'command_status' : {'service': item.item , 'status': item.failed }}, recursive=True) }}"
with_items: "{{ excecute.results }}"
- name:
debug:
msg: "{{ hostvars['localhost'] | json_query('service_status') }}"
我得到的輸出是
任務 [調試]
ok: [localhost] => {
"msg": {
"command_status": {
"service": "world",
"status": false
}
}
}
但我想要像下面這樣的輸出
"command_status": [
{
"service": "hello",
"status": false
},
{
"service": "world",
"status": false
}
]
循環不是創建具有選定結果屬性的字典列表所必需的。 相反,使用json_query multiselect hash並創建字典列表cmd_stats 。 然后一步結合字典。 例如下面的任務
- name: Setting facts
set_fact:
service_status: "{{ service_status|
combine({'command_status': cmd_stats}) }}"
vars:
cmd_stats: "{{ excecute.results|
json_query('[].{service: item, status: failed}') }}"
- debug:
var: service_status
給
service_status:
command_status:
- service: hello
status: false
- service: world
status: false
筆記:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.