# Adding to a list multiple times with set\_fact

**URL:** <https://forum.ansible.com/t/adding-to-a-list-multiple-times-with-set-fact/19213>\
**Category:** Ansible Project\
**Created:** [September 9, 2015, 12:14pm UTC](https://forum.ansible.com/t/adding-to-a-list-multiple-times-with-set-fact/19213 "2015-09-09T12:14:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neech\_WORTHINGTON](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neech\_WORTHINGTON](https://forum.ansible.com/u/Neech_WORTHINGTON)\
**Post date:** [September 9, 2015, 12:14pm UTC](https://forum.ansible.com/t/adding-to-a-list-multiple-times-with-set-fact/19213/1 "2015-09-09T12:14:06Z")

</div>

Greetings fellow Ansible users!

I have a use case where I want to add values to a list dependant on host groups. I will then use the resulting list as an argument to another task.

It’s important than I don’t have duplicate values in the list, so I am using union() to add new items to the list, as indicated by [http://docs.ansible.com/ansible/playbooks\_filters.html](http://docs.ansible.com/ansible/playbooks_filters.html)

For some reason, as soon as a set\_fact task is run against the same host and variable for a second time, Ansible explodes with an unintuitive Python/Jinja2 error, saying it’s getting a list when it doesn’t expect a list (which makes NO sense to me, because union() wants two lists are arguments surely?!).

> **fatal: [host4-uk] =\> Failed to template fruit=“{{ fruit | union([‘Goosberry’, ‘Blackberry’]) }}”: an unexpected type error occurred. Error was coercing to Unicode: need string or buffer, list found**

Can anybody shed any light on this? Am I doing something particularly silly? Is there a better way to achieve what I want to do?

To illustrate the problem, I have tried to distill the issue down to this simple example:

**nicola $ ls -1 \***  
**fruit.yaml**  
**inventory**

**group\_vars:**  
**all**

**nicola $ cat group\_vars/all**  
**fruit:**  
**- UniversalFruit1-Foo**

**nicola $ cat inventory**  
**[tropical]**  
**host1-asia ansible\_ssh\_host=172.18.32.89**  
**host2-asia ansible\_ssh\_host=172.18.97.157**

**[domestic]**  
**host3-uk ansible\_ssh\_host=172.18.128.12**  
**host4-uk ansible\_ssh\_host=172.18.128.13**

**[berries]**  
**host4-uk ansible\_ssh\_host=172.18.128.13**

**nicola $ cat fruit.yaml**

---

<div class="post-metadata">

**Author:** ![trupti\_mali](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@trupti\_mali](https://forum.ansible.com/u/trupti_mali)\
**Post date:** [September 13, 2015, 4:40pm UTC](https://forum.ansible.com/t/adding-to-a-list-multiple-times-with-set-fact/19213/2 "2015-09-13T16:40:24Z")

</div>

Hello,  
How did you resolve this problem. I too faced the same thing but no resolution yet.

~Trupti
