【问题标题】:Create user in ansible在ansible中创建用户
【发布时间】:2020-03-28 07:45:50
【问题描述】:

我正在尝试使用 ansible 更新 /etc/passwd 文件。

当我在/etc/passwd 中搜索用户时,我找不到它。
但是当我执行命令 id username 时,它会显示带有 uid 和 gid 等的输出。

所以为了更新/etc/passwd,我在ansible中使用了user模块。

- name: Ensure user is present
  user:
    name: abc
    uid: 1111
    group: 2222
    state: present
  register: os_bin_user_created

显示任务正常

ok: [lxxxxxx33.xxxxxxxxx.com] => {
    "append": false, 
    "changed": false, 
    "comment": "abc", 
    "group": 2222, 
     ....
            "password": null, 
            "password_lock": null, 
            "remove": false, 
            "uid": "1111", 
            "update_password": "always"
        }
    }, 
    "move_home": false, 
    "name": "abc", 
    "shell": "/bin/ksh", 
    "state": "present", 
    "uid": 1111
}

但是/etc/passwd 仍然没有更新。如何解决这个问题? 如果我尝试使用新用户 xyz,其中 id 命令输出状态用户不存在,则此代码有效(即它更新 /etc/passwd)

【问题讨论】:

  • 您使用什么操作系统?在 Linux 上 user 模块执行 useradd 命令,它可能与 Ansible 无关。此外,该用户是否存在于/etc/shadow/etc/group 文件中?我还会仔细检查您是否检查了正确节点上的文件(在您的示例中为lxxxxxx33.xxxxxxxxx.com)。
  • RHEL 7 是操作系统,是的,我正在检查 corect 服务器
  • /etc/shadow 和 /etc/group 也没有此用户的任何内容
  • 如果您的系统配置为使用除/etc/passwd 以外的目录源(例如 ldap、nis 等),如果用户 abc 存在于这些其他源之一中,您会期望这种行为.
  • 如果getent passwd abc 为您的用户返回一个条目,那么这不是 Ansible 问题,而是完全是系统配置问题。

标签: ansible rhel7


【解决方案1】:

对于这种情况,组和用户已经在外部目录服务中定义,但本地映射是必要的,对于不符合要求的组名(即包含空格字符),可以使用以下方法。

- name: Create 'default group' locally in /etc/group
  lineinfile:
    path: "/etc/group"
    line: 'default group:x:1234:'
    state: present

它确保域默认组可用,否则无法在 RHEL 7 和操作系统工具中创建。

- name: Create 'default user' locally in /etc/passwd
  lineinfile:
    path: "/etc/passwd"
    line: 'default user:x:1234:1234::/home/default user:/bin/bash'
    state: present

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多