有人可以在以下方面为我提供帮助:
任一
ok: [localhost] => {
"tag_info": {
"changed": false,
"msg": "All items completed",
"results": [
{"vm_list": [ "NSY6TFSANSBL01"]},
]
OR
ok: [localhost] => {
"tag_info": {
"changed": false,
"msg": "All items completed",
"results": [
{"vm_list": [ "NSY6TFSANSBL01"]},
{"vm_list": [ "NSY6TFSANSBL01,NSY6TFSANSBL02"]},
{"vm_list": [ "NSY6TFSANSBL01,NSY6TFSANSBL02,NSY6TFSANSBL03"]},
]
Intersection of N lists works with the code
- set_fact:
final_list: "{{ final_list|
default(tag_info.results.0.vm_list)|
intersect(tag_info.results[item].vm_list) }}"
loop: "{{ range(1, tag_info.results|length, 1)|list }}"
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++]
我希望final_list是{“ vm_list”:[“ NSY6TFSANSBL01”]},当只有一个vm_list对象时,它应该是多个列表的交集。你能让我知道怎么写吗?
这样写
- set_fact:
final_list: "{{ (tag_info | json_query('results[*].vm_list')|flatten(levels=1)|unique if tag_info.results|length > 1) | default(tag_info.results.0.vm_list) }}"
loop: "{{ tag_info.results }}"
- debug:
var: final_list
假设:
每个问题的所有vm_list对象都是数组中的单个字符串。
我假设不是,它实际上是如下所示的字符串列表,
ok: [localhost] => {
"tag_info": {
"changed": false,
"msg": "All items completed",
"results": [
{"vm_list": [ "NSY6TFSANSBL01"]},
]
OR
ok: [localhost] => {
"tag_info": {
"changed": false,
"msg": "All items completed",
"results": [
{"vm_list": [ "NSY6TFSANSBL01"]},
{"vm_list": [ "NSY6TFSANSBL01","NSY6TFSANSBL02"]},
{"vm_list": [ "NSY6TFSANSBL01","NSY6TFSANSBL02","NSY6TFSANSBL03"]},
]