Create different users on different hosts

Hi,

I’ve got various servers to manage which belong to different departments. Also one server could belong to multiple departments.
I would like to create the admin users on all servers, and additional users depending on the departments that the server belongs to (like development).
All of this by looping over group_var files which contain the user information.

Simplified example:

inventory file:
file “hosts”:
[servers]
srv1
srv2

[dep1]
srv2

[dep2]
srv2

group_vars:
file “all”:
users:
admin1:
comment: “Admin user 1”

file “dep1”:
users:
dep1:
comment: “Department1 user”

file “dep2”:
users:
dep2:
comment: “Department2 user”

task:
file “users.yml”:

  • name: Create users
    user:
    name: “{{item.key}}”
    comment: “{{item.value.comment}}”
    with_dict: “{{ users }}”

So “srv2” belongs to groups “all”, “dep1” and “dep2”, hence the users “admin1”, “dep1” and “dep2” should be created on “srv2”
But only user “dep2” will be created, because this is the last file through which ansible is looping in alphabetical order.
If I would rename the file “dep1” to “ZZZdep1” this would be the last file, and user “dep1” would be created.

I can’t find a way to achieve this, any help appreciated.

Cheers,
Frank

You could have a look at hash_behaviour = merge
https://docs.ansible.com/ansible/intro_configuration.html#hash-behaviour