背景
生产工作需要,新增了服务器,加入到了ansible的hosts文件,使用ad-hoc命令执行测试连接异常。
环境说明
项目 | 版本 | 备注 |
---|---|---|
Centos | 7.9.2009 (Core) | 被控端系统 |
Ubuntu | 18.04 | 主控端系统 |
ansible | 2.9.27 | 工具 |
vi /etc/ansible/hosts
[ops]
10.6.6.161 ansible_ssh_user=[user_name] ansible_ssh_pass=[xxxxxxxx] ansible_become_method=sudo ansible_sudo_pass=[xxxxxxxx]
排查步骤
- 使用的普通用户连接连接错误异常,
10.6.6.161 | FAILED! => { │======================================================
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Invalid/incorrect password: Permission denied, please try again.",
"unreachable": true
}
- 切换到管理员操作命令还是错误异常。
修改hosts文件
[ops]
10.6.6.161 ansible_ssh_user=root ansible_ssh_pass=[xxxxxxxx]
10.6.6.161 | FAILED! => { │======================================================
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to 10.6.6.161 closed.\r\n",
"module_stdout": "/bin/sh: sudo: 未找到命令\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127
}
- 登录到远程电脑上操作
sudo mkdir /data
bash-: no sudo model
定位
服务器没有sudo模块
处理步骤
安装sudo
yum install sudo -y
验证
重新执行ansible命令,用户为普通用户。
ansible ops -m ping
j@TK:~$ ansible ops -m ping
10.6.6.161 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
异常-sudo: 需要密码
登录阿里云服务器使用过程中的报错
ssh-copy-id -i /home/username/.ssh/ansible.pub username@192.168.1.114
提示输入分发端服务器的shalter账号密码
输入完毕后,在分发端的
#/home/username/.ssh/生成authorized_keys文件
并且在文件中存储了主控端的rsa值,具备以下数据后,从主控端可以免密登陆分发端主机。
vi /etc/ansible/hosts
[ali]
XXX.XX.XX.XX ansible_ssh_user=username
主控端输入
ansible ali -m ping
弹出如下异常
xx.xxx.xxx.xxx | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "Shared connection to xx.xxx.xxx.xx closed.\r\n",
"module_stdout": "sudo: 需要密码\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
登录被控端服务器
#visudo
root ALL=(ALL) ALL
username ALL=(ALL) NOPASSWD: ALL
:wq
保存退出
重新执行
$ ansible ali -m ping
xx.xxx.xxx.xx | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}