在ansible中创建用户

问题描述 投票:0回答:1

我正在尝试使用 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

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)

ansible rhel7
1个回答
0
投票

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

- 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
© www.soinside.com 2019 - 2024. All rights reserved.