ansible的故障排除
配置ansible日志
查看日志是排除故障的一个最有效的方法。
# 编辑ansible的配置文件
vim ansible.cfg
# 在[defaults]下日志ansible日志路径
[defaults]
# 默认的路径下可能要root写权限
#log_path = /var/log/ansible.log
log_path = ./ansible.log
通过ANSIBLE_LOG_PATH
变量来设置log路径
# 通过变量设置log路径
ANSIBLE_LOG_PATH = /home/student/role-system/ansible.log
# 查看变量的值
echo $ANSIBLE_LOG_PATH
# 取消log路径变量
unset ANSIBLE_LOG_PATH
使用debug模块调试变量
- name: 打印变量值
debug:
msg: "变量值是 {{ 变量值 }}"
- name: 显示output变量
debug:
var: output
verbosity: 2 # 详细级别2相当于vv
使用ansible-playbook语法检查
只能检查语法错误,不能检查逻辑错误
# 检查语法错误
ansible-plybook play.yml --syntax-check
单步调试
就是一个tasks一个tasks运行
# 不要对role使用--step
[student@workstation role-system]$ ansible-playbook test.yml --step
PLAY [user system role] ******************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue:
# tasks单步调试,指定tasks开始运行--step
[student@workstation tasks]$ ansible-playbook palybook1.yml --start-at-task=“tasks名称“