# Looking for alternative to set\_fact + loop

**URL:** <https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204>\
**Category:** Ansible Project\
**Created:** [October 11, 2022, 2:05pm UTC](https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204 "2022-10-11T14:05:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 11, 2022, 2:05pm UTC](https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204/1 "2022-10-11T14:05:42Z")

</div>

Hi

I have a recurring challenge in our deployment, whereby we have a list  
structure that is defaulted to a single (dict) item.  
The challenge is that I would like to extend that in a way so that  
additional items use the keys of the first item as their default.  
I have gotten this work as in the example playbook:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 11, 2022, 2:32pm UTC](https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204/2 "2022-10-11T14:32:20Z")

</div>

I thought i had it:

vars:  
&nbsp;&nbsp;discos: "{{ discos\_extra|map('combine', disco\_default) }}"

But it is the wrong precedence, need to think about how to reverse that.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 11, 2022, 3:57pm UTC](https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204/3 "2022-10-11T15:57:09Z")

</div>

For example, given the default dictionary and the extra list

&nbsp;&nbsp;&nbsp;&nbsp;defaults:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: default  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;desc: standard  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bar: true

&nbsp;&nbsp;&nbsp;&nbsp;extra:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: foobar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;desc: dedicated  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: noli  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;desc: discovery

the declaration below

&nbsp;&nbsp;&nbsp;&nbsp;discos: "{{ [defaults] +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[defaults]|product(extra)|map('combine')|list }}"

gives the expected list

&nbsp;&nbsp;discos:  
&nbsp;&nbsp;- bar: true  
&nbsp;&nbsp;&nbsp;&nbsp;desc: standard  
&nbsp;&nbsp;&nbsp;&nbsp;foo: true  
&nbsp;&nbsp;&nbsp;&nbsp;name: default  
&nbsp;&nbsp;- bar: true  
&nbsp;&nbsp;&nbsp;&nbsp;desc: dedicated  
&nbsp;&nbsp;&nbsp;&nbsp;foo: true  
&nbsp;&nbsp;&nbsp;&nbsp;name: foobar  
&nbsp;&nbsp;- bar: true  
&nbsp;&nbsp;&nbsp;&nbsp;desc: discovery  
&nbsp;&nbsp;&nbsp;&nbsp;foo: true  
&nbsp;&nbsp;&nbsp;&nbsp;name: noli

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 11, 2022, 8:07pm UTC](https://forum.ansible.com/t/looking-for-alternative-to-set-fact-loop/37204/4 "2022-10-11T20:07:48Z")

</div>

Perfect!!!  
V much appreciated
