YAML 列表过滤
YAML List Filtering
有人可以帮助我解决以下问题:
或者
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 }}"
+++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++
当只有一个 vm_list 对象时,我希望 final_list 为 {"vm_list": [ "NSY6TFSANSBL01"]} 否则它应该是多个列表的交集.
你能告诉我怎么写吗?
这样写
- 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"]},
]
有人可以帮助我解决以下问题:
或者
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 }}"
+++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++
当只有一个 vm_list 对象时,我希望 final_list 为 {"vm_list": [ "NSY6TFSANSBL01"]} 否则它应该是多个列表的交集. 你能告诉我怎么写吗?
这样写
- 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"]},
]