如何使用通配符进行ansible输出
How to use wildcard for ansible output
我正在尝试为我的 ansible 变量使用通配符,但我似乎无法使用它。
我试过 here 的东西,但还是一样。
ansible 输出
"reboot_required": false,
"updates": {
"0720a128-90b1-4b21-a8cf-3c5c86239435": {
"kb": [
"2267602"
],
"installed": false,
"id": "0720a128-90b1-4b21-a8cf-3c5c86239435",
"categories": [
"Definition Updates",
"Windows Defender"
],
"title": "Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.412.0)"
},
"60bbf4af-afd3-45fe-aad2-6d72beddeba2": {
"kb": [
"4509475"
],
"installed": false,
"id": "60bbf4af-afd3-45fe-aad2-6d72beddeba2",
"categories": [
"Updates",
"Windows Server 2016"
],
"title": "2019-06 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4509475)"
我正在尝试获取标题或 ID
- name: debug
debug:
msg: "{{ item.updates.*.id }}"
with_items:
- "{{ result }}"
感谢帮助
他在您链接的示例中使用的通配符是 json_query 过滤器的一部分。他正在使用 json_query 过滤器,然后使用通配符作为该语法的一部分。
results | json_query('[].block_device_mapping.*.snapshot_id')
您没有在您的示例中使用 json_query,因此,此语法不可用且不会起作用。
尝试将您的结果传送到 json_query,然后包括您想要到达的路径。如果 {{ results }} 已经创建,你可以离开 with_items 并使用类似的东西:
{{ results | json_query('updates.*.id') }}
我在这里猜测确切的语法,但您肯定必须从 json_query 开始。
要弄清楚你想要的确切语法,开始小管道到 json_query 然后抓住最上面的元素(更新,在你的情况下),向过滤器添加片段直到你缩小范围到你想要的信息。我已经链接到下面的探路者,它有帮助。
参考:
编辑:Vladimir 回答的第一部分中的语法看起来比我猜测的更性感。尝试他的语法来了解什么是有效的,使用我的答案来理解哪里出了问题。然后标记他为正确答案。
鉴于上面的 ansible 输出 存储在变量 result 下面的任务
- set_fact:
id_list: "{{ result.updates|
json_query('*.id')
}}"
- debug:
var: id_list
给出id的列表(类似titles)
id_list:
- 0720a128-90b1-4b21-a8cf-3c5c86239435
- 60bbf4af-afd3-45fe-aad2-6d72beddeba2
以及下面的任务
- set_fact:
my_list: "{{ result.updates|
json_query('*.{ id: id, title: title }')
}}"
- debug:
var: my_list
给出 id, title hashes
的列表
my_list:
- id: 0720a128-90b1-4b21-a8cf-3c5c86239435
title: Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.412.0)
- id: 60bbf4af-afd3-45fe-aad2-6d72beddeba2
title: 2019-06 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4509475)
在下面试试。不过我还没有测试过。
- name: debug
debug:
msg: "{{ item|first }}:{{ item[item|first].title }}"
with_items:
- "{{ result.updates }}"
我正在尝试为我的 ansible 变量使用通配符,但我似乎无法使用它。
我试过 here 的东西,但还是一样。
ansible 输出
"reboot_required": false,
"updates": {
"0720a128-90b1-4b21-a8cf-3c5c86239435": {
"kb": [
"2267602"
],
"installed": false,
"id": "0720a128-90b1-4b21-a8cf-3c5c86239435",
"categories": [
"Definition Updates",
"Windows Defender"
],
"title": "Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.412.0)"
},
"60bbf4af-afd3-45fe-aad2-6d72beddeba2": {
"kb": [
"4509475"
],
"installed": false,
"id": "60bbf4af-afd3-45fe-aad2-6d72beddeba2",
"categories": [
"Updates",
"Windows Server 2016"
],
"title": "2019-06 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4509475)"
我正在尝试获取标题或 ID
- name: debug
debug:
msg: "{{ item.updates.*.id }}"
with_items:
- "{{ result }}"
感谢帮助
他在您链接的示例中使用的通配符是 json_query 过滤器的一部分。他正在使用 json_query 过滤器,然后使用通配符作为该语法的一部分。
results | json_query('[].block_device_mapping.*.snapshot_id')
您没有在您的示例中使用 json_query,因此,此语法不可用且不会起作用。
尝试将您的结果传送到 json_query,然后包括您想要到达的路径。如果 {{ results }} 已经创建,你可以离开 with_items 并使用类似的东西:
{{ results | json_query('updates.*.id') }}
我在这里猜测确切的语法,但您肯定必须从 json_query 开始。
要弄清楚你想要的确切语法,开始小管道到 json_query 然后抓住最上面的元素(更新,在你的情况下),向过滤器添加片段直到你缩小范围到你想要的信息。我已经链接到下面的探路者,它有帮助。
参考:
编辑:Vladimir 回答的第一部分中的语法看起来比我猜测的更性感。尝试他的语法来了解什么是有效的,使用我的答案来理解哪里出了问题。然后标记他为正确答案。
鉴于上面的 ansible 输出 存储在变量 result 下面的任务
- set_fact:
id_list: "{{ result.updates|
json_query('*.id')
}}"
- debug:
var: id_list
给出id的列表(类似titles)
id_list:
- 0720a128-90b1-4b21-a8cf-3c5c86239435
- 60bbf4af-afd3-45fe-aad2-6d72beddeba2
以及下面的任务
- set_fact:
my_list: "{{ result.updates|
json_query('*.{ id: id, title: title }')
}}"
- debug:
var: my_list
给出 id, title hashes
的列表 my_list:
- id: 0720a128-90b1-4b21-a8cf-3c5c86239435
title: Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.412.0)
- id: 60bbf4af-afd3-45fe-aad2-6d72beddeba2
title: 2019-06 Cumulative Update for Windows Server 2016 for x64-based Systems (KB4509475)
在下面试试。不过我还没有测试过。
- name: debug
debug:
msg: "{{ item|first }}:{{ item[item|first].title }}"
with_items:
- "{{ result.updates }}"