How to append to list vars

64 views
Skip to first unread message

Dick Visser

unread,
Nov 1, 2017, 7:45:27 AM11/1/17
to ansible...@googlegroups.com
Hi

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


users:
- allen
- bob
- chris
- 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:
- allen
- bob
- chris
- dick
- 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!!


--
Dick Visser
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

Kai Stian Olstad

unread,
Nov 1, 2017, 2:23:16 PM11/1/17
to ansible...@googlegroups.com
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([])) }}


--
Kai Stian Olstad

Arbab Nazar

unread,
Nov 2, 2017, 2:55:00 PM11/2/17
to Ansible Project

Dick Visser

unread,
Nov 13, 2017, 1:12:57 AM11/13/17
to ansible...@googlegroups.com
Thanks but I prefer the union approach as that takes care of removing any double values.

Dick

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/938f2541-e78b-424c-8aab-bb40834a57fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages