允许用户在使用 Ansible 创建帐户后设置密码

Allow user to set password after creating account using Ansible

我有以下剧本,我用它在 Ubuntu 16.04 主机上创建新用户:

---
- hosts: all 
  become: yes 
  become_method: sudo
  tasks:
  - name: Create user guybrush
    user: name=guybrush comment="Guybrush Threepwood" shell=/bin/bash groups=pirates append=yes

  - name: Copy SSH key for guybrush
    authorized_key: user=guybrush key={{ lookup("file", "/home/guybrush/.ssh/id_rsa.pub") }}

然而,当我以新用户身份登录并尝试使用 passwd 设置我的密码时,我被要求输入我的(当前)UNIX 密码。鉴于没有为用户设置密码,我不确定为什么要求我输入密码。

如何修复我的 playbook,以便新创建的用户在首次登录后可以轻松设置密码?

您可以将密码设置为空行,因此passwd不会询问旧密码。
您也可以将其设置为过期以强制用户在首次登录时更改密码。

- user: name=jsmith password=""
- authorized_key: user=jsmith key={{ lookup("file", "/somepath/id_rsa.pub") }}
- command: chage -d 0 jsmith

如果空密码存在安全问题,您可以将其设置为某个随机的预定义密码,并播放欢迎 ssh 消息以在首次登录时显示它。