我是 Ansible 的新手,我尝试在远程服务器上创建一些用户帐户,但遇到了一些麻烦。 如果用户不存在,我想创建用户,如果存在,我想更新他们的密码。
我阅读了文档并找到了参数“update_password”,但我仍然停留在如何验证它们的存在上。
我试着这样做:
- name: Determine local user accounts
getent:
database: passwd
- name: Add user
user:
name: support
comment: support account
password: bonjour
groups: support,pricing
append: yes
with_items: {{ user }}
when: user not in ansible_facts.getent_passwd
- name: Update user password
user:
name: support
password: bonjour
update_password: always
with_items: {{ user }}
when: user in ansible_facts.getent_passwd
我不确定是否理解 ansible_facts 的概念。
最佳答案
Ansible 的一个关键基础是它围绕 idempotency 构建.这意味着您只需描述您希望系统处于的状态,然后让 Ansible 确定需要做什么的详细信息,以使您的系统符合您的期望状态。
因此,您只需在系统上定义您想要的用户,Ansible 将负责检查它们是否已经存在,并采取相应的行动:
- name: Manage support user
user:
name: support
comment: support account
password: <some crypted password string>
groups: support,pricing
append: yes
如果用户不存在,这将添加用户,否则更新用户参数以匹配您的规范。
注意您不应在这些任务中放置明文密码。 Checkout this page有关如何创建加密密码的详细信息。
关于linux - 如果用户存在 Ansible 则更新密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665362/