--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuX20ndh8%2Bh_oCx8nGGBJTZ4oeEFfGQb0tWy6MXAuhTucA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/03325950-FBD3-49F2-82BC-485D290BDEE1%40nist.gov.
aggregates:- { cluster: "Cluster 1", aggregate: "aggr1" }- { cluster: "Cluster 1", aggregate: "aggr2" }- { cluster: "Cluster 1", aggregate: "aggr4" }- { cluster: "Cluster 2", aggregate: "aggr1" }- { cluster: "Cluster 3", aggregate: "aggr2" }
---
- name: test loop with JSON dict list
hosts: all
become: false
gather_facts: false
vars:
aggregates:
- { cluster: "Cluster 1", aggregate: "aggr1" }
- { cluster: "Cluster 1", aggregate: "aggr2" }
- { cluster: "Cluster 1", aggregate: "aggr4" }
- { cluster: "Cluster 2", aggregate: "aggr1" }
- { cluster: "Cluster 3", aggregate: "aggr2" }
tasks:
- name: show reference
debug: msg="cluster {{ item.cluster }}, hostname {{ item.aggregate }}"
loop: "{{ aggregates }}"
% ansible-playbook -i localhost, netapp.yml
PLAY [test loop with JSON dict list] ***********************************************************************************
TASK [show reference] **************************************************************************************************
ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr1'}) => {
"msg": "cluster Cluster 1, hostname aggr1"
}
ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr2'}) => {
"msg": "cluster Cluster 1, hostname aggr2"
}
ok: [localhost] => (item={'cluster': 'Cluster 1', 'aggregate': 'aggr4'}) => {
"msg": "cluster Cluster 1, hostname aggr4"
}
ok: [localhost] => (item={'cluster': 'Cluster 2', 'aggregate': 'aggr1'}) => {
"msg": "cluster Cluster 2, hostname aggr1"
}
ok: [localhost] => (item={'cluster': 'Cluster 3', 'aggregate': 'aggr2'}) => {
"msg": "cluster Cluster 3, hostname aggr2"
}
PLAY RECAP *************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuXJdpJe7gBCCY1nQYxhz44DuQA4V7c5qVXAakPCe_-hEA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3006506F-DEBA-4596-BF35-765F7DA80939%40nist.gov.
- name: convert list of lists to list of dicts
set_fact:
json_list: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') | map('community.general.dict') }}"
"json_list": [
{
"aggregate": "aggr1",
"cluster": "Cluster 1"
},
{
"aggregate": "aggr2",
"cluster": "Cluster 1"
},
{
"aggregate": "aggr4",
"cluster": "Cluster 1"
},
{
"aggregate": "aggr1",
"cluster": "Cluster 2"
},
{
"aggregate": "aggr2",
"cluster": "Cluster 3"
}
]
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuVhbZtfcH8cYjaM1%2BsypwfbHQK1P_QFQozw3%3DKOizudrA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/BFABBF7B-29C9-4299-B0A8-3F6FCB1EE68D%40nist.gov.
- name: convert list of lists to list of dicts 1
set_fact:
json_list: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') }}"
loop: "{{ aggregates2 }}"
- name: convert list of lists to list of dicts 2
set_fact:
new_list: "{{ (new_list | default([])) + [ { item.0.0: item.0.1, item.1.0: item.1.1 } ] }}"
loop: "{{ json_list }}"
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuWVoLVV%2BQJU0AC9fSn%2BCpnPp%2BETkMpCV61fuVt_UbhFhg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/464F213D-3BF5-401F-ADC3-E917E992DDFC%40nist.gov.
---
- name: test loop with JSON dict list
hosts: all
become: false
gather_facts: false
vars:
aggregates:
- { cluster: "Cluster 1", aggregate: "aggr1" }
- { cluster: "Cluster 1", aggregate: "aggr2" }
- { cluster: "Cluster 1", aggregate: "aggr4" }
- { cluster: "Cluster 2", aggregate: "aggr1" }
- { cluster: "Cluster 3", aggregate: "aggr2" }
aggregates2: [
[ "Cluster 1", "aggr1" ],
[ "Cluster 1", "aggr2" ],
[ "Cluster 1", "aggr4" ],
[ "Cluster 2", "aggr1" ],
[ "Cluster 3", "aggr2" ]
]
tasks:
- name: convert list of lists to list of dicts 1
set_fact:
json_list1: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') | map('community.general.dict') }}"
json_list2: "{{ aggregates2 | map('zip',['cluster','aggregate']) | map('map','reverse') }}"
loop: "{{ aggregates2 }}"
- name: convert list of lists to list of dicts 2
set_fact:
json_list3: "{{ (json_list3 | default([])) + [ { item.0.0: item.0.1, item.1.0: item.1.1 } ] }}"
loop: "{{ json_list2 }}"
- name: show reference
debug:
msg:
- "{{ json_list1 }}"
- "{{ json_list2 }}"
- "{{ json_list3 }}"
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuU_-FeRf3vkwHS2OApGxp8JmhPzSstUb0DTBPEqfQemDQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6051A287-FFAC-416E-B098-85A767239113%40nist.gov.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuWT_KQcqw2mHHMcoWhNKkP5v25qHYudKjyXhtjxy8uP1Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/AF580880-6BE1-4F34-853E-BF41847E0AE7%40nist.gov.