问题
- 通过ansible-playbook批量管理服务器,需要事先编写inventory文件,目标服务器均使用随机密码。
- 在使用ansible-playbook和ansible命令时,均出现其中一台无法登录成功的问题。
- 通过ssh命令,使用inventory中记录得用户名、密码测试ssh登录,则成功无误。
报错如下:
[sysma@ansible mysql-glibc]$ ansible -i mysqlproSP3.ini 172.22.89.138 -m ping
172.22.89.138 | UNREACHABLE! => {
"changed": false,
"msg": "Invalid/incorrect password: \nAuthorized users only. All activities may be monitored and reported.\nPermission denied, please try again.",
"unreachable": true
}
原因
随机密码出现了\转义字符,导致inventory文件内的密码在传递时发生转移,因此ansible命令使用的是错误密码。
排查
将随机密码修改为无\符号的密码,再次测试ansible命令成功无误。
解决
将密码还原,修改inventory文件内容,将标记密码字段的双引号修改为单引号,ansible默认对单引号内的内容不进行转义处理。
173.33.89.76 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass="xxxxxxxxxj\0=>" become_user="ptmauser" ansible_become_pass="xxxxxxxxxj\0=>"
173.33.89.77 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass="xxxxxxxxxowgzm" become_user="ptmauser" ansible_become_pass="xxxxxxxxxowgzm"
173.33.89.138 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass="xxxxxxxxx3ez>\" become_user="ptmauser" ansible_become_pass="xxxxxxxxx3ez>\"
173.33.89.139 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass="xxxxxxxxx6XziQ" become_user="ptmauser" ansible_become_pass="xxxxxxxxx6XziQ"
将其中随机密码出现转义字符的此行进行修改,从
173.33.89.138 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass="xxxxxxxxx3ez>\" become_user="ptmauser" ansible_become_pass="xxxxxxxxx3ez>\"
改为
173.33.89.138 ansible_port=22 ansible_user="ptmauser" ansible_ssh_pass='xxxxxxxxx3ez>\' become_user="ptmauser" ansible_become_pass='xxxxxxxxx3ez>\'
再次验证ansbile命令成功。