# set facts from items in a loop

**URL:** <https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 3, 2023, 9:58pm UTC](https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297 "2023-08-03T21:58:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solarflow\_99](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@solarflow\_99](https://forum.ansible.com/u/solarflow_99)\
**Post date:** [August 3, 2023, 9:58pm UTC](https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297/1 "2023-08-03T21:58:23Z")

</div>

I wonder if something like this can be done, the problem i’m seeing is that the fact name has to be static, and could not use {{ item }}:

- name: Set each tag to facts  
set\_fact:  
list\_of\_vars: “{{ my\_instance.instances | map(attribute=‘tags.list\_of\_vars’) | list | join(‘\n’) }}”

- name: Split “list\_of\_vars” variable into a comma separated list  
set\_fact:  
item\_list: “{{ list\_of\_vars.split(‘,’) }}”

- name: lookup SSM Parameter Store secrets  
set\_fact:  
**{{ item }}:** “{{ lookup(‘amazon.aws.ssm\_parameter’, puppet\_env+‘_‘+env+’_{{ item }}’, region=ansible\_ec2\_placement\_region) }}”  
with\_items: “{{ item\_list }}”

This would give me a fact for each comma separated var I put in the tag, if only it worked. Is there a better way to do something like this?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 3, 2023, 11:24pm UTC](https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297/2 "2023-08-03T23:24:17Z")

</div>

You could perhaps template your desired variables and values into a file, then use the ansible.builtin.include\_vars module to read them in.  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include\_vars\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_vars_module.html)

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [August 4, 2023, 7:00am UTC](https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297/3 "2023-08-04T07:00:00Z")

</div>

I don’t 100% understand your ultimate aim here. It might be easier to understand if you break it down into smaller steps and include some sample inputs and desired output?

A sanitised version of what’s in my\_instance, item\_list , and what you would like out at the end might help?

IF I understand what you’re trying to do, using your loop item as the key for your set\_fact. For me, with appropriate quoting this seems to work…

$ cat wtf.yml

- hosts: localhost  
become: no  
gather\_facts: no

vars:  
varlist:

- value1
- value2
- valueC

tasks:

- name: Use item as the fact key for set\_fact in a loop  
set\_fact:  
“{{ item }}”: value  
loop: “{{ varlist }}”

- name: Check the values assigned to the facts  
debug:  
var: “{{ item }}”  
loop: “{{ varlist }}”

$ ansible-playbook wtf.yml  
PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TASK [Use item as the fact key for set\_fact in a loop] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=value1)  
ok: [localhost] =\> (item=value2)  
ok: [localhost] =\> (item=valueC)

TASK [Check the values assigned to the facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=value1) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “value1”,  
“value1”: “value”  
}  
ok: [localhost] =\> (item=value2) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “value2”,  
“value2”: “value”  
}  
ok: [localhost] =\> (item=valueC) =\> {  
“ansible\_loop\_var”: “item”,  
“item”: “valueC”,  
“valueC”: “value”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![solarflow\_99](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@solarflow\_99](https://forum.ansible.com/u/solarflow_99)\
**Post date:** [August 4, 2023, 11:03pm UTC](https://forum.ansible.com/t/set-facts-from-items-in-a-loop/38297/4 "2023-08-04T23:03:06Z")

</div>

doh, thats the only thing it took I was missing! If I double quoted {{item}} like you just did, then it works.

To give some more background on the problem, I couldn’t simply template the vars since each of my different host inventories may have a different number of variables it needed. So my aim was to do something like this, Create a tag on an EC2 instance with a comma separated list I would use to loop through, for each item it would assign a value from parameter store that matches with the item name:

TAG:

Key Value

list\_of\_vars = a,b,c

in param store I have a parameter of “a” “b” and “c” with a secret value assigned. This way all I have to do is update the tag with more variables, and each host can have whatever I want as long as there is a matching param store. Its working now, this is great, thanks to everyone.

The only other way I could think of would have to say “for this inventory file, use this var template” and I don’t think there is any good way to do that.
