报错
[root@server1 ~]# ansible-playbook ping.yaml #此时发现运行tasks:ping不成功
PLAY [mysql] **********************************************************************************
TASK [Gathering Facts] ************************************************************************
ok: [20.0.0.13]
TASK [useradd mysql] **************************************************************************
ok: [20.0.0.13]
TASK [ping] ***********************************************************************************
fatal: [20.0.0.13]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}
PLAY RECAP ************************************************************************************
20.0.0.13 : ok=2 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
①在进行ansible-playbook对MySQL主机操作时提示ping任务不成功
②解决的思路
1、根据报错信息"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)."
显示,权限被拒绝
2、检查ssh密钥是否需要重传
3、运行的任务的用户权限是否能够进行ping操作
解决
最终确定,是用户权限不能够进行ping操作
解决过程
- 给mysql用户提权
[root@server1 opt]# vim ping.yaml
- hosts: mysql
remote_user: root
become:
tasks:
- name: ping
ping:
remote_user: mysql
[root@server1 ~]# ansible-playbook ping.yaml
PLAY [mysql] **********************************************************************************
TASK [Gathering Facts] ************************************************************************
fatal: [20.0.0.13]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"setup": {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "failed": true, "module_stderr": "Shared connection to 20.0.0.13 closed.\r\n", "module_stdout": ">>> /etc/sudoers: 语法错误 near line 1 <<<\r\nsudo: /etc/sudoers 中第 1 行附近有解析错误\r\nsudo: 没有找到有效的 sudoers 资源,退出\r\nsudo: 无法初始化策略插件\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}}, "msg": "The following modules failed to execute: setup\n"}
PLAY RECAP ************************************************************************************
20.0.0.13 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
运行结果仍然出错
提示/etc/sudoers 中第 1 行附近有解析错误\r\nsudo: 没有找到有效的 sudoers 资源
继续解决
在被控制端sudoers中添加运行用户
[root@server1 ~]# vim ping.yaml
- hosts: mysql
remote_user: root
tasks:
- name: useradd mysql
user: name=mysql #创建一个mysql用户
- name: sudoers
shell: /usr/bin/echo "mysql ALL=(root) ALL" >> /etc/sudoers
- name: ping
become: yes #允许进行提权
become_user: mysql #提权账号
ping:
将运行用户添加进sudosers中再次运行
[root@server1 ~]# ansible-playbook ping.yaml
PLAY [mysql] **********************************************************************************
TASK [Gathering Facts] ************************************************************************
ok: [20.0.0.13]
TASK [useradd mysql] **************************************************************************
ok: [20.0.0.13]
TASK [sudoers] ********************************************************************************
changed: [20.0.0.13]
TASK [ping] ***********************************************************************************
ok: [20.0.0.13]
PLAY RECAP ************************************************************************************
20.0.0.13 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
解决!!!