# How to define host var and append values to it from task

**URL:** <https://forum.ansible.com/t/how-to-define-host-var-and-append-values-to-it-from-task/19798>\
**Category:** Ansible Project\
**Created:** [November 18, 2015, 9:46pm UTC](https://forum.ansible.com/t/how-to-define-host-var-and-append-values-to-it-from-task/19798 "2015-11-18T21:46:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elad\_Yosef](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@Elad\_Yosef](https://forum.ansible.com/u/Elad_Yosef)\
**Post date:** [November 18, 2015, 9:46pm UTC](https://forum.ansible.com/t/how-to-define-host-var-and-append-values-to-it-from-task/19798/1 "2015-11-18T21:46:53Z")

</div>

Hi all,I’m now to ansible

I have host variable

locked\_domains:

and I’m trying to append values to it from task

- set\_fact: locked\_domains=“{{ locked\_domains + item.ID }}”  
with\_items: “{{ domains.json }}”

Can anyone help?

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [November 19, 2015, 11:04pm UTC](https://forum.ansible.com/t/how-to-define-host-var-and-append-values-to-it-from-task/19798/2 "2015-11-19T23:04:32Z")

</div>

I think the short answer is that you don’t. Ansible has a setting that will allow you to merge dictionaries together, but not lists. This setting is global and applies to all dicts. I’m guessing it doesn’t merge lists because there’s no way to indicate if your intent is to merge the list or replace it.

Your best bet, here, is to write a filter\_plugin that merges the lists.

e.g.

`locked_domains|merge_list(domains.json,'ID')`

where merge\_list() is a function in a filter\_plugin that takes 3 arguments: original\_list, new\_listofdicts, key.

There might be with\_ statement that does this, but looking at your task, I’m not sure if any will do exactly what you want (depends on how you’re going to use locked\_domains).

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 21, 2015, 1:12am UTC](https://forum.ansible.com/t/how-to-define-host-var-and-append-values-to-it-from-task/19798/3 "2015-11-21T01:12:28Z")

</div>

You can try this:

list1|union(list2)

it will output a list with the unique elements from both lists.
