我正在尝试从json数组中设置事实,因为密钥包含我无法解析的空间,有人可以在这里帮我吗, 我想将事实设置为“名称”:“ IN-FG-04”,而“ vdom”:“ vdom-shop”
请参阅我的示例剧本条目
- name: Iterate JSON
set_fact:
app_item: "{{ item['scope member'] }}"
with_items: "{{ result.results }}"
register: app_result
请参阅json输入,这是我上一个任务的输出
{
"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"
}
]
}
}
答案 0 :(得分:0)
注意:尽管这对于练习循环来说是一个很好的练习,但是您绝对应该考虑修正给出结果或write your own filter的上一个任务。
首先,您正在register
任务上使用set_fact
。它本身不是错误,但是兴趣非常有限。如果您为变量正确分配了一个值,而不是值本身,这将注册。因此,您将永远无法获得预期的结果。
您要做的是在set_fact
任务中遍历json时将项目追加到列表中。这是通过在第一个循环上使用default
过滤器将您的set_fact
变量初始化为一个空列表并向其中添加新列表来完成的。
此外,由于您需要同时查看对象的多个级别以进行决策,因此需要使用subelements
循环:每个结果上的级别为0,循环中每个元素的级别为1。 scope member
列表。在每次迭代中,您都将一个对象附加到列表中。在下面的示例中,我使用了ternary
过滤器来确定要包括的名称。
---
- name: SO Test
hosts: localhost
vars:
result: {
"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"
}
]
}
tasks:
- name: Get all names + vdom as requested in a list
set_fact:
app_result: >-
{{
app_result
| default([])
+
[{
'name': (item.1.vdom == 'vdom-shop') | ternary(item.0.name, item.1.name),
'vdom': item.1.vdom
}]
}}
loop: "{{ lookup('subelements', result.results, 'scope member') }}"
- name: show result
debug:
var: app_result
产生的结果
PLAY [SO Test] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Get all names + vdom as requested in a list] *****************************
ok: [localhost] => (item=[{'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'}, 'type': 'pkg'}, {'name': 'IN-FG-04', 'vdom': 'vdom-shop'}])
ok: [localhost] => (item=[{'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'}, 'type': 'pkg'}, {'name': 'IN-FG-04', 'vdom': 'vdom1-dc'}])
TASK [show result] *************************************************************
ok: [localhost] => {
"app_result": [
{
"name": "FG-04-Policy",
"vdom": "vdom-shop"
},
{
"name": "IN-FG-04",
"vdom": "vdom1-dc"
}
]
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
答案 1 :(得分:0)
下面的代码段
将事实设置为“名称”:“ IN-FG-04”,而“ vdom”:“ vdom-shop”
- name: "set fact as name: IN-FG-04 when vdom: vdom-shop"
set_fact:
name: "{{ result.results|json_query('[].\"scope member\"[?\"vdom\"==`\"vdom-shop\"`].name')|flatten|first }}"
- debug:
var: name
给予:
"name": "IN-FG-04"