How to refer to an existing uid and gid in Ansible?

Solution 1:

See the Ansible module getent with database=passwd.

Solution 2:

While Bruce P's answer is often a good solutions in some situations there is no way to just supply a name. Following Satish Koppisetty's approach here is some code to do it:

- name: get myuser uid
  getent:
    database: passwd
    key: myuser

- name: get mygroup gid
  getent:
    database: group
    key: mygroup

You now have two dictionaries (getent_passwd and getent_group), from which you can retrieve the data. The following code just outputs the ids:

- debug:
    msg:
      - "user id {{ getent_passwd.myuser[1] }}"
      - "group id {{ getent_group.mygroup[1] }}"

A tiny bit of background: this is the case because getent returns a dictionary that looks something like this:

{
    "mygroup": [
        "x",
        "1004",
        "some_group_member"
    ]
}

Solution 3:

You should be able to just specify uid=amavis-user,gid=amavis-group in your /etc/fstab. The linux mount program will interpret them correctly.

Tags:

Linux

Ansible