大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

In my playbook I install the amavis package. Later in a task I use the mount module to setup a ramdisk (tmpfs). In order to let the tmpfs-ramdisk be owned by the uid and gid created during amavis package install (amavis-user and …

问题原文

原帖链接 · initall · CC BY-SA 3.0

In my playbook I install the amavis package. Later in a task I use the mount module to setup a ramdisk (tmpfs).

In order to let the tmpfs-ramdisk be owned by the uid and gid created during amavis package install (amavis-user and amavis-group, in /etc/fstab then using the options parameter) how do I find out which uid and gid was created?

Goal (something like):

/dev/shm /var/lib/amavis/tmp tmpfs defaults,noexec,nosuid,nodev,noatime,size=500m,mode=770,uid=112,gid=116 0 0

Or should I create the users before package installation to explicitly set uid and gid via Ansible?

社区高分解答

原帖链接 · H2O · CC BY-SA 4.0

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"
    ]
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-12-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。