Question:
I am trying to shrink several chunks of similar code which looks like:
1 2 3 4 5 6 7 |
- ... multiple things is going here register: list_register - name: Generating list set_fact: my_list="{{ list_register.results | map(attribute='ansible_facts.list_item') | list }}" # the same code repeats... |
The only difference between them is list name instead of my_list
.
In fact, I want to do this:
1 2 3 |
set_fact: "{{ some var }}" : "{{ some value }}" |
Is it possible to do so or is there any workaround?
Answer:
take a look at this sample playbook:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
--- - hosts: localhost vars: iter: - key: abc val: xyz - key: efg val: uvw tasks: - set_fact: {"{{ item.key }}":"{{ item.val }}"} with_items: "{{iter}}" - debug: msg="key={{item.key}}, hostvar={{hostvars['localhost'][item.key]}}" with_items: "{{iter}}" |