linux - 如果用户存在 Ansible 则更新密码

标签 linux ansible

我是 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/

相关文章:

c - 如何在循环中设置信号检查?

linux - 使用主目录 linux 创建新用户

ansible - Ansible uri 模块中的多个 status_code

ansible - 如何评估 Ansible 插件中的模板表达式?

linux - Ubuntu 的 crontab 脚本不工作

linux - 如何从 tcsh 中的 LD_LIBRARY_PATH 中删除路径?

linux - Shell 命令不适用于 TinyPNG 的压缩照片

deployment - 如何在 Ansible 中跳过角色执行

security - 如何阻止 Ansible 中的 extra_vars 覆盖变量?

ubuntu - Ansible 无法找到 requirements.txt