大网数据广告活动
服务器运维问答

Via ansible change password of non-root account using passwd command and picking old password from ansible files

· 52CE 我爱测

I have got a lot of systems having a common username to login. The password is also same for most of the systems. I have 1 inventory file with all the systems. For the systems that have a different password, I have saved the pass…

问题原文

原帖链接 · Aseem · CC BY-SA 3.0

I have got a lot of systems having a common username to login. The password is also same for most of the systems. I have 1 inventory file with all the systems. For the systems that have a different password, I have saved the password against the hostname, and for the remaining it is in the group_vars file.

In inventory file:

hostname1 ansible_ssh_pass=pass1
hostname2 ansible_ssh_pass=pass2
hostname3
hostname4

group_vars file:

ansible_user: someuser
ansible_ssh_pass: pass3

I do not have higher priviledges. From another thread I was able to figure out how to only use passwd to change the password, and not use other utilities.

shell: 'printf "%s\n" OldPass NewPass NewPass | passwd'

The passwd does not support the "-p" option.

I am trying to figure out a way to get the OldPass value from the same where ansible is picking it for each host so that I do not have to run this playbook n number of times, and manually put the OldPass in the shell command.

Please advice on how to proceed with this.

Thanks.

已采纳解答

原帖链接 · dawud · CC BY-SA 3.0

You can use variables directly:

$ 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

In this example, the variables are fed to ansible from the command line, in your example, they would come from group_vars.

You can even define a default value and override it for specific systems:

$ 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。