# Ansible Lists and Sets problem working with Set types in Ansible

**URL:** <https://forum.ansible.com/t/ansible-lists-and-sets-problem-working-with-set-types-in-ansible/15295>\
**Category:** Ansible Project\
**Created:** [May 21, 2014, 3:32pm UTC](https://forum.ansible.com/t/ansible-lists-and-sets-problem-working-with-set-types-in-ansible/15295 "2014-05-21T15:32:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Hobbs](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@Ryan\_Hobbs](https://forum.ansible.com/u/Ryan_Hobbs)\
**Post date:** [May 21, 2014, 3:32pm UTC](https://forum.ansible.com/t/ansible-lists-and-sets-problem-working-with-set-types-in-ansible/15295/1 "2014-05-21T15:32:54Z")

</div>

I am attempting to create a unique list of values between two lists and I am creating a set based on the documentation described here [http://docs.ansible.com/playbooks\_variables.html#set-theory-filters](http://docs.ansible.com/playbooks_variables.html#set-theory-filters)

According to the documentation:

To get a union of two lists:

```auto
{{ list1 | union(list2) }}

```

Therefore I am attempting this in my playbook:

- name: A  
set\_fact:  
listA: “{{test1.stdout.split()}}”  
listB: “{{test2.stdout.split()}}”

Test1 and Test2 output a list object and have been verified. The above produces two lists as an example lets assume:

listA = [‘apple’, ‘orange’, ‘tree’, ‘pear’]  
listB = [‘tree’, ‘orange’]

Now I want to get a union of both listA and listB

- name: B  
set\_fact: test=“{{listA | union(listB) }}”

This produces the result below:

{“ansible\_facts”: {“test”: “set([“tree”, “orange”])”}, “item”: “”}

My problem is the fact test is assigned a string value of “set([“tree”, “orange”])” and not the set object, I need to operate on the set object. Can this be done? If not is there any other way within Ansible to get a unique list from two lists?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 21, 2014, 8:50pm UTC](https://forum.ansible.com/t/ansible-lists-and-sets-problem-working-with-set-types-in-ansible/15295/2 "2014-05-21T20:50:22Z")

</div>

that looks wrong to me, that is what i would expect from intersect, from union i would expect “test” to be equal to listA (which includes listB).

Aside from that, {{}} tells jinja to convert to string by default (not sure how you get set() at all), you can do {{test|list}} to ensure you get the correct type  
​
