![通过 ansible 使用 passwd 命令更改非 root 帐户的密码,并从 ansible 文件中获取旧密码](https://linux22.com/image/683534/%E9%80%9A%E8%BF%87%20ansible%20%E4%BD%BF%E7%94%A8%20passwd%20%E5%91%BD%E4%BB%A4%E6%9B%B4%E6%94%B9%E9%9D%9E%20root%20%E5%B8%90%E6%88%B7%E7%9A%84%E5%AF%86%E7%A0%81%EF%BC%8C%E5%B9%B6%E4%BB%8E%20ansible%20%E6%96%87%E4%BB%B6%E4%B8%AD%E8%8E%B7%E5%8F%96%E6%97%A7%E5%AF%86%E7%A0%81.png)
我有很多系统使用通用用户名登录。大多数系统的密码也相同。我有一个包含所有系统的清单文件。对于密码不同的系统,我已将密码保存在主机名下,其余系统的密码保存在 group_vars 文件中。
在库存文件中:
hostname1 ansible_ssh_pass=pass1
hostname2 ansible_ssh_pass=pass2
hostname3
hostname4
group_vars 文件:
ansible_user: someuser
ansible_ssh_pass: pass3
我没有更高的权限。从另一个线程中,我能够弄清楚如何仅使用 passwd 来更改密码,而不使用其他实用程序。
shell: 'printf "%s\n" OldPass NewPass NewPass | passwd'
passwd 不支持“-p”选项。
我正在尝试找出一种方法来从 ansible 为每个主机选择 OldPass 值的地方获取 OldPass 值,这样我就不必运行这个剧本 n 次,并手动将 OldPass 放入 shell 命令中。
请就如何进行此事提供建议。
谢谢。
答案1
您可以直接使用变量:
$ ansible -c local -m shell \
-a "printf '%s\n' {{ ansible_ssh_pass }} {{ ansible_ssh_newpass }}" \
--extra-vars="ansible_ssh_pass=oldpass ansible_ssh_newpass=newpass" \
localhost
127.0.0.1 | SUCCESS | rc=0 >>
oldpass
newpass
在这个例子中,变量是ansible
从命令行输入的,在你的例子中,它们来自group_vars
。
您甚至可以定义一个默认值并为特定系统覆盖它:
$ ansible -c local -m shell \
-a "printf '%s\n' {{ ansible_ssh_pass | default('otherpass') }} {{ ansible_ssh_newpass }}" \
--extra-vars="ansible_ssh_newpass=newpass" \
localhost
127.0.0.1 | SUCCESS | rc=0 >>
otherpass
newpass