我有以下输出:
"current_profiles": [
{
"context": "client-side",
"full_path": "/Common/clientssl",
"name": "clientssl"
},
我尝试将其修改为这样:
"current_profiles": [
- name : "client-side",
context : "/Common/clientssl",
]
答案 0 :(得分:0)
下面的任务创建一个只有一项的列表 current_profiles
vars:
current_profiles:
-
context: client-side
full_path: /Common/clientssl
name: clientssl
tasks:
- set_fact:
current_profiles: "{{ current_profiles|
map('dict2items')|
map('rejectattr', 'key', 'match', 'full_path')|
map('list')|
map('items2dict')|
list }}"
- debug:
var: current_profiles
- debug:
var: current_profiles[0]
提供JSON输出
ok: [localhost] => {
"current_profiles": [
{
"context": "client-side",
"name": "clientssl"
}
]
}
ok: [localhost] => {
"current_profiles[0]": {
"context": "client-side",
"name": "clientssl"
}
}
在YAML中是等效的
current_profiles:
- context: client-side
name: clientssl
current_profiles[0]:
context: client-side
name: clientssl
请参见YAML is a superset of JSON ... every JSON file is also a valid YAML file。