and the user attributes are all set as variables and the variables for these users are being picked up from roles/common/vars/main.yml
I also have user variables defined under group_vars/dev and group_vars/prd
And I also have specific users defined at host level under host_vars/server1
Now the problem is that Ansible is only picking up users from one level, whilst I want to pick up all the user variables from all levels, i.e. sysadmins, dev users & specific host users, and add them all in, not just from the most preferred variable source.
In Puppet this is achieved by using Hiera and “hiera_hash” which allows variable values to get collectively applied from all hiera levels.
With the approach above, the code would merge together all lists that match a pattern, this way you can defined as many "users" variables as you want as long as they are unique and start with say "users_"
If you don't want to copy the library or wait for the PR, then you could also just merge a set of predefined lists.
So how can I combine variables for a list of “users” defined like so:
group_vars/all
users:
name: alice
comment: Alice
group_vars/group1
users:
name: bob
comment: Bob
host_vars/host1
users:
name: charlie
comment: Charlie
How can all these variables be combined using “loop” or “with_items”?
What if there are duplicates?
Can the “host_vars/host1” variables override and take precedence over the “group_vars/all” variables?
They have solved exactly this issue and I'm testing their plugin and
so far so good. Certainly not as elegant as Puppet/Hiera but it's a
working solution.
I agree though, I am also coming from puppet and miss hiera. I may still consider going back to using it as there appears to be support for it, but it would be nice to be fully integrated
The strategy I ended up going with for the similar problem you have is
I created a “users” hash in vars/ which contains all users that can be managed
then in my host & group_vars I populate a list such as “linux_users_global” or linux_users_unique_name which contains the list of users that should be managed either globally, group or at the host level
then in my playbook I merge all the variables that match the pattern “linux_users.*” . (see this link)
then I loop through that merged list and manage those users