Host_vars for users

Hi,

I’m really new to ansible and I’m still learning but I’m stuck and need your help.
I want to use host_vars for a list of users to deploy them to a host.

Let’s say we have three users (with it ssh keys) and 2 machines

Joe, Jack and John

srv1 and srv2

Now I’ld like to realize that on srv1 only joe and jack will be deployed and on srv2 all three of them should be deployed. But all users should be maintained in one user.yml file to keep maintenance on usernames and ssh key’s low.

How do I need to declare that in the host_vars file? Can I reference there to my users.yml file? If yes how?

_Shorty

Lots of ways to slice this up, but you either have to reference (directly or indirectly) the hosts from the users, or the users from the hosts.

I might create my one users.yml as group_vars/all/users.yml so the user data would be available to all hosts. Then the data might look like this:

---
# group_vars/all/users.yml
project_host_users:
  - name: Joe Jones
    username: joe
    uid: 1345
    gid: 1345
    pub_key: "........"
    host_groups:  # zero or more host groups
      - web_dev
      - web_tst
    hosts: []  # zero or more hosts
  - name: Jack Spratt
    username: jack
    uid: 1346
    gid: 1346
    pub_key: "........"
    host_groups: []  # zero or more host groups
    hosts:  # zero or more hosts
      - srv1
      - srv2

Your role that deploys users onto hosts can filter out the ones appropriate for each host.

There are lots of other ways you could do it. It’s just a matter of which one you’re more comfortable maintaining.

2 Likes