内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
风哥提示:
本文档介绍Ansible常见故障的排查方法。
Part01-连接故障排查
1.1 SSH连接问题
[root@ansible ~]# ansible webservers -m ping -vvv
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_check()
Parsed /etc/ansible/hosts inventory source with script plugin
META: ran handlers
<192.168.1.20> ESTABLISH SSH CONNECTIOfrom PG视频:www.itpux.comN FOR USER: root
<192.168.1.20> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s
…
web1.fgedu.net.cn | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
# 常见SSH连接错误及解决方法
[root@ansible ~]# cat > /fglinux/ansible/troubleshooting/ssh_issues.txt << 'EOF'
SSH连接常见问题排查
==================
1. Host key verification failed
原因: 主机密钥验证失败
解决:
- 在ansible.cfg中设置 host_key_checking = False
- 或手动ssh连接一次接受密钥
2. Permission denied (publickey,password)
原因: 认证失败
解决:
- 检查用户名密码是否正确
- 检查SSH密钥是否配置
- 检查sshd配置允许的认证方式
3. Connection refused
原因: SSH服务未运行或端口错误
解决:
- 检查目标主机SSH服务状态
- 确认SSH端口配置正确
4. Connection timed out
原因: 网络不通或防火墙阻止
解决:
- 检查网络连通性
- 检查防火墙规则
- 增加连接超时时间
5. Python interpreter not found
原因: 目标主机未安装Python
解决:
- 安装Python: dnf install python3
- 或在inventory中指定解释器路径
EOF
# 测试SSH连接
[root@ansible ~]# ansible webservers -m ping -u root -k
SSH password:
web1.fgedu.net.cn | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python3”
},
“changed”: false,
“ping”: “pong”
}
# 使用SSH密钥连接
[root@ansible ~]# ssh-copy-id root@192.168.1.20
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
Number of key(s) added: 1
[root@ansible ~]# ansible webservers -m ping
web1.fgedu.net.cn | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
Part02-Playbook故障排查
2.1 语法检查
[root@ansible ~]# ansible-playbook /fglinux/ansible/playbooks/test.yml –syntax-check
playbook: /fglinux/ansible/playbooks/test.yml
# 详细模式执行
[root@ansible ~]# ansible-playbook /fglinux/ansible/playbooks/test.yml -vvv
# 检查模式执行
[root@ansible ~]# ansible-playbook /fglinux/ansible/playbooks/test.yml –check
# 常见Playbook错误
[root@ansible ~]# cat > /fglinux/ansible/troubleshooting/playbook_issues.txt << 'EOF'
Playbook常见错误排查
==================
1. YAML语法错误
错误信息: syntax error while loading YAML
解决方法:
- 使用yamllint检查语法
- 注意缩进(使用空格而非Tab)
- 检查引号匹配
2. 变量未定义
错误信息: The task includes an undefined variable
解决方法:
- 检查变量定义位置
- 使用default过滤器设置默认值
- 使用when条件判断变量是否存在
3. 模块参数错误
错误信息: unsupported parameter for module
解决方法:
- 查阅官方文档确认参数
- 检查参数拼写
- 确认模块版本支持
4. 权限不足
错误信息: Permission denied
解决方法:
- 添加become: yes提权
- 检查sudo配置
- 确认用户权限
5. 任务失败处理
解决方法:
- 使用ignore_errors忽略错误
- 使用block/rescue处理异常
- 使用failed_when自定义失败条件
EOF
# 使用block/rescue处理错误
[root@ansible ~]# cat > /fglinux/ansible/playbooks/error_handling.yml << 'EOF'
---
- name: 错误处理演示
hosts: webservers
become: yes
tasks:
- block:
- name: 尝试启动服务
service:
name: nonexistent-service
state: started
rescue:
- name: 服务启动失败处理
debug:
msg: "服务不存在,跳过启动"
always:
- name: 始终执行的任务
debug:
msg: "任务执行完成"
- name: 使用ignore_errors
command: /bin/false
ignore_errors: yes
- name: 使用failed_when
shell: cat /etc/nonexistent
register: result
failed_when: result.rc != 0 and 'No such file' not in result.stderr
- name: 使用changed_when
command: echo "test"
register: output
changed_when: false
EOF
# 执行错误处理Playbook
[root@ansible ~]# ansible-playbook /fglinux/ansible/playbooks/error_handling.yml
PLAY [错误处理演示] **********************************************************
TASK [Gathering Facts] ******************************************************
ok: [web1.fgedu.net.cn]
TASK [尝试启动服务] **********************************************************
fatal: [web1.fgedu.net.cn]: FAILED! => {“changed”: false, “msg”: “Could not find the requested service nonexistent-service: host”}
TASK [服务启动失败处理] ******************************************************
ok: [web1.fgedu.net.cn] => {
“msg”: “服务不存在,跳过启动”
}
TASK [始终执行的任务] ********************************************************
ok: [web1.fgedu.net.cn] => {
“msg”: “任务执行完成”
}
TASK [使用ignore_errors] ****************************************************
fatal: [web1.fgedu.net.cn]: FAILED! => {“changed”: true, “cmd”: [“/bin/false”], “delta”: “0:00:00.001234”, “end”:更多学习教程公众号风哥教程itpux_com “2026-04-04 03:30:00.123456”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2026-04-04 03:30:00.123456”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
…ignoring
TASK [使用failed_when] *************************************************学习交流加群风哥微信: itpux-com*****
ok: [web1.fgedu.net.cn]
TASK [使用changed_when] ****************************************************
ok: [web1.fgedu.net.cn]
PLAY RECAP *****************************学习交流加群风哥QQ113257174*************************************
web1.fgedu.net.cn : ok=6 changed=1 unreachable=0 failed=0
- 使用-vvv获取详细日志
- 先用–check检查模式
- 使用–syntax-check验证语法
- 合理处理任务失败
- 记录错误信息便于分析
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
