Is it possible to convert list of primitives to list of dicts using Jinja2 using list/map comprehensions?
Given this structure:
list:
- some_val
- some_val_2
Apply map
on every element to obtain:
list:
- statically_added: some_val
- statically_added: some_val_2
It is possible other way around: list_from_example|map(attribute="statically_added")|list
I came up with the same question and found this solution:
- debug:
msg: "{{ mylist | json_query('[].{\"statically_added\": @}') }}"
vars:
mylist:
- some_val
- some_val_2
Output:
ok: [localhost] => {
"msg": [
{
"statically_added": "some_val"
},
{
"statically_added": "some_val_2"
}
]
}
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