问题描述
我正在尝试从json数组中设置事实,因为密钥包含我无法解析的空间,有人可以在这里帮我吗, 我想将事实设置为名称":"IN-FG-04",而"vdom":"vdom-shop"
i am trying to set facts from a json array, since the key contains space i am unable to parse, can someone help me here, i want to set fact as "name": "IN-FG-04" when "vdom": "vdom-shop"
请参阅我的示例剧本条目
Please see my sample playbook entry
- name: Iterate JSON
set_fact:
app_item: "{{ item['scope member'] }}"
with_items: "{{ result.results }}"
register: app_result
请参阅json输入,这是我上一个任务的输出
please see the json input and this is an output of my previous task
{
"msg": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"failed": false,
"msg": "Custom Query Success",
"results": [
{
"name": "FG-04-Policy",
"obj ver": 3,
"oid": 1196,
"package settings": {
"central-nat": "disable",
"fwpolicy-implicit-log": "disable",
"fwpolicy6-implicit-log": "disable",
"inspection-mode": "proxy"
},
"scope member": [
{
"name": "IN-FG-04",
"vdom": "vdom-shop"
}
],
"type": "pkg"
},
{
"name": "FG-04-DC",
"obj ver": 23,
"oid": 1216,
"package settings": {
"central-nat": "disable",
"fwpolicy-implicit-log": "disable",
"fwpolicy6-implicit-log": "disable",
"inspection-mode": "proxy"
},
"scope member": [
{
"name": "IN-FG-04",
"vdom": "vdom1-dc"
}
],
"type": "pkg"
}
]
}
}
推荐答案
注意:尽管这对于练习循环来说是一个很好的练习,但您绝对应该考虑修正给出结果的前一个任务,或者第一个循环上的default
过滤器,并在其中添加新列表.
What you want to do is to append items to a list while you are looping over your json in the set_fact
task. This is done by initializing your set_fact
variable to an empty list with the default
filter on first loop and appending a new list with your items.
此外,由于您需要同时查看对象的多个级别以做出决定,因此需要使用