# set\_fact with\_items inside var

**URL:** <https://forum.ansible.com/t/set-fact-with-items-inside-var/29604>\
**Category:** Ansible Project\
**Created:** [February 3, 2019, 4:45pm UTC](https://forum.ansible.com/t/set-fact-with-items-inside-var/29604 "2019-02-03T16:45:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [February 3, 2019, 4:45pm UTC](https://forum.ansible.com/t/set-fact-with-items-inside-var/29604/1 "2019-02-03T16:45:47Z")

</div>

I need to add an item to a list. This works:

- name: “Build Pool IP List”  
set\_fact:  
pool\_server\_ips\_list: “{{pool\_server\_ips\_list}} + [{‘ip’: {‘addr’: item, ‘type’: ‘V4’}}]”  
with\_items: “{{pool\_server\_ips}}”

with this list:

pool\_server\_ips:

- 10.8.10.4
- 10.8.10.5
- 10.8.10.6
- 10.8.10.7

I need to add another element to the list so I changed it to:

pool\_server\_ips:

- hostip: 10.8.10.4  
pool\_member\_enabled: true
- hostip: 10.8.10.5  
pool\_member\_enabled: false
- hostip: 10.8.10.6  
pool\_member\_enabled: false
- hostip: 10.8.10.7

pool\_member\_enabled: false

then changed fact to:

set\_fact:  
pool\_server\_ips\_list: “{{pool\_server\_ips\_list}} + [{‘ip’: {‘addr’: item.hostip, ‘type’: ‘V4’}}]”  
with\_items: “{{pool\_server\_ips}}”

If I debug the list i get:

“servers”: [  
“ + [{‘ip’: {‘addr’: [item.hostip]”,  
" ‘type’: ‘V4’}}] + [{‘ip’: {‘addr’: [item.hostip]“,  
" ‘type’: ‘V4’}}] + [{‘ip’: {‘addr’: [item.hostip]”,  
" ‘type’: ‘V4’}}] + [{‘ip’: {‘addr’: [item.hostip]“,  
" ‘type’: ‘V4’}}]”  
]

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [February 3, 2019, 8:03pm UTC](https://forum.ansible.com/t/set-fact-with-items-inside-var/29604/2 "2019-02-03T20:03:32Z")

</div>

> I need to add an item to a list. This works:
> 
> - name: "Build Pool IP List"  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;pool\_server\_ips\_list: "{{pool\_server\_ips\_list}} + [{'ip': {'addr':  
> item, 'type': 'V4'}}]"  
> &nbsp;&nbsp;with\_items: "{{pool\_server\_ips}}"
> 
> with this list:
> 
> pool\_server\_ips:  
> &nbsp;&nbsp;- 10.8.10.4  
> &nbsp;&nbsp;- 10.8.10.5  
> &nbsp;&nbsp;- 10.8.10.6  
> &nbsp;&nbsp;- 10.8.10.7
> 
> I need to add another element to the list so I changed it to:
> 
> pool\_server\_ips:  
> &nbsp;&nbsp;- hostip: 10.8.10.4  
> &nbsp;&nbsp;&nbsp;&nbsp;pool\_member\_enabled: true  
> &nbsp;&nbsp;- hostip: 10.8.10.5  
> &nbsp;&nbsp;&nbsp;&nbsp;pool\_member\_enabled: false  
> &nbsp;&nbsp;- hostip: 10.8.10.6  
> &nbsp;&nbsp;&nbsp;&nbsp;pool\_member\_enabled: false  
> &nbsp;&nbsp;- hostip: 10.8.10.7  
> &nbsp;&nbsp;&nbsp;&nbsp;pool\_member\_enabled: false
> 
> then changed fact to:
> 
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool\_server\_ips\_list: "{{pool\_server\_ips\_list}} + [{'ip': {'addr':  
> item.hostip, 'type': 'V4'}}]"

Try:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool\_server\_ips\_list: "{{pool\_server\_ips\_list | default() + [{'ip': {'addr': item.hostip, 'type': 'V4'}}] }}"

> &nbsp;&nbsp;with\_items: "{{pool\_server\_ips}}"
> 
> If I debug the list i get:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"servers": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + [{'ip': {'addr': [item.hostip]",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 'type': 'V4'}}] + [{'ip': {'addr': [item.hostip]",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 'type': 'V4'}}] + [{'ip': {'addr': [item.hostip]",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 'type': 'V4'}}] + [{'ip': {'addr': [item.hostip]",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 'type': 'V4'}}]"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]

V/r,  
James Cassell

---

<div class="post-metadata">

**Author:** ![Tony\_Owens](https://avatars.discourse-cdn.com/v4/letter/t/58956e/32.png) [@Tony\_Owens](https://forum.ansible.com/u/Tony_Owens)\
**Post date:** [February 3, 2019, 8:21pm UTC](https://forum.ansible.com/t/set-fact-with-items-inside-var/29604/3 "2019-02-03T20:21:12Z")

</div>

You are a champion! Thank you so much!
