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

43 views
Skip to first unread message

Elad Yosef

unread,
Nov 18, 2015, 5:33:08 PM11/18/15
to Ansible Project
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?

Mike Biancaniello

unread,
Nov 19, 2015, 6:04:33 PM11/19/15
to Ansible Project
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).

Brian Coca

unread,
Nov 20, 2015, 8:12:35 PM11/20/15
to Ansible Project
You can try this:

list1|union(list2)

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


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages