How to append to list vars

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!!

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()) }}

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

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

Dick