# How to append to list vars

**URL:** <https://forum.ansible.com/t/how-to-append-to-list-vars/25884>\
**Category:** Ansible Project\
**Created:** [November 1, 2017, 11:44am UTC](https://forum.ansible.com/t/how-to-append-to-list-vars/25884 "2017-11-01T11:44:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 1, 2017, 11:44am UTC](https://forum.ansible.com/t/how-to-append-to-list-vars/25884/1 "2017-11-01T11:44:53Z")

</div>

Hi

I have a few use cases where I define a list in group\_vars/all.yml,  
for instance a list of users:

users:  
&nbsp;&nbsp;- allen  
&nbsp;&nbsp;- bob  
&nbsp;&nbsp;- chris  
&nbsp;&nbsp;- dick

So this list applies to all hosts in my case.

But for a specific host, I want to add one entry to the list.  
The only way I can get that to work is by copying the list in  
host\_vars/somehost/main.yml and just manually add it:

users:  
&nbsp;&nbsp;- allen  
&nbsp;&nbsp;- bob  
&nbsp;&nbsp;- chris  
&nbsp;&nbsp;- dick  
&nbsp;&nbsp;- extrauser

Obviously this is not ideal as any changes made to the first var will  
have to be manually carried over to the specific instances.

Is there some way to append items to list vars (or subtract, for that matter)?

I was hoping to be able to use something like:

users: "{{ users }} + ['extrauser']"

But that gives me "recursive loop detected in template string".

Any other/clean ways to solve this?

Thanks!!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 1, 2017, 6:22pm UTC](https://forum.ansible.com/t/how-to-append-to-list-vars/25884/2 "2017-11-01T18:22:47Z")

</div>

Use two variable users and extra\_users on host that need to have some extra users.

Then you can use this, because of default this will not fail on host that do not have extra\_users varaible set.

{{ users | union(extra\_users | default()) }}

---

<div class="post-metadata">

**Author:** ![Arbab\_Nazar1](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Arbab\_Nazar1](https://forum.ansible.com/u/Arbab_Nazar1)\
**Post date:** [November 2, 2017, 6:55pm UTC](https://forum.ansible.com/t/how-to-append-to-list-vars/25884/3 "2017-11-02T18:55:00Z")

</div>

Dick, something like this:  
[http://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible](http://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2017, 6:12am UTC](https://forum.ansible.com/t/how-to-append-to-list-vars/25884/4 "2017-11-13T06:12:32Z")

</div>

Thanks but I prefer the union approach as that takes care of removing any double values.

Dick
