I have a variable in the form:
vars:
somelist:
- abc: 1
def: 'somestring1'
- abc: 2
def: 'somestring2'
- abc: 3
def: 'somestring3'
and I'd like to pass the following list to some role's variable:
- import_role:
name: somerole
vars:
somevar:
- '/somestring1/1/'
- '/somestring2/2/'
- '/somestring3/3/'
how can I map the objects of somelist to the string /{{ def }}/{{ abc }}/ and pass the resulting list to somevar?
In Ansible, the task below does the job
- set_fact:
somevar: "{{ somevar|default([]) +
[ '/' ~ item.def ~ '/' ~ item.abc ~ '/'] }}"
loop: "{{ somelist }}"
- set_fact:
somevar: "{{ somelist|json_query('[].[``,def,abc,``]')|
map('join','/')|
list }}"
gives
somevar:
- /somestring1/1/
- /somestring2/2/
- /somestring3/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With