通过使用passwd命令的非root帐户的可靠更改密码并从可执行文件中select旧密码

我有很多系统有一个共同的用户名login。 大多数系统的密码也是一样的。 我有1个库存文件与所有系统。 对于具有不同密码的系统,我已经将密码保存在主机名上,其余的保存在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”选项。

我试图找出一种方法来获得来自每个主机select它的同一个OldPass值,以便我不必运行该剧本n次,并手动将OldPass放在shell命令中。

请咨询如何继续。

谢谢。

你可以直接使用variables:

 $ 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 

在这个例子中,variables是从命令行input到的,在你的例子中,它们来自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