Reputation: 143
Could someone please assist me on the below:
Either
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 }}"
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I would want the final_list to be {"vm_list": [ "NSY6TFSANSBL01"]} when there is only one vm_list object if not it should intersection of multiple lists. Could you please let me know how to write that.
Upvotes: 0
Views: 477
Reputation: 596
Write it like so
- 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
Assumptions:
all vm_list objects as per question is single string in an array.
I am assuming it is not that and it is actually a list of strings as below,
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"]},
]
Upvotes: 1