ansible concatenate variables

22 views
Skip to first unread message

Krzysztof Kułaj

unread,
Mar 29, 2019, 11:14:18 AM3/29/19
to Ansible Project
Hello, 

I have simple question about concatenating variables in Ansible.

I am using a role https://github.com/singleplatform-eng/ansible-users . I would like to define root user in group_vars/all . It works well.

The problem is, when I would like to specify additional users for group of machines, or single host. If I define users variable, then the users in group_vars/all variable is not seen. 

Is there any way to concatenate the variables in this scenerio?

Best Regards, 

Krzysztof Kulaj 

Brian Coca

unread,
Apr 3, 2019, 5:35:48 PM4/3/19
to Ansible Project
Variables are lazy loaded so :

vars:
x: '{{ x + [1] }}'

will not work since the value of x is not known before you try to use
x creating a self referential loop.

But you can get around it with `set_fact` as we evaluate variables at
the time of module execution and set_fact is a module that sets a new
variable:

set_fact:
x : '{{ x + [1] }}'

this defines a 'new x' that is the 'old x + [1]' (add a list with the
number 1 as an element, assumes x is already a list, you might use
|default([]) if you are not assured x was previously defined


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