背景
由于工作中经常用到ansible,所以整理了常用的ansible错误及原因分析,方便自己也方便别人参考。
1.shell 模块常见错误
1.1 使用shell遇到"msg": “non-zero return code”
ansible 脚本如下:
- name: Check the weblogic without wc
shell: “ps -ef|grep weblogic|grep -v grep”
register: check_weblogic0
ignore_errors: true
ansible 返回错误:
TASK [Check the weblogic without wc] *********************************************************************************************************************************************************************************************************fatal: [robin.org.cn]: FAILED! => {“changed”: true, “cmd”: “ps -ef|grep weblogic|grep -v grep”, “delta”: “0:00:00.036565”, “end”: “2020-02-23 18:08:03.100106”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2020-02-23 18:08:03.063541”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
…ignoring
ok: [robin.org.cn] => {
“msg”: {
“changed”: true,
“cmd”: “ps -ef|grep weblogic|grep -v grep”,
“delta”: “0:00:00.036565”,
“end”: “2020-02-23 18:08:03.100106”,
“failed”: true,
“msg”: “non-zero return code”,
“rc”: 1,
“start”: “2020-02-23 18:08:03.063541”,
“stderr”: “”,
“stderr_lines”: [],
“stdout”: “”,
“stdout_lines”: []
}
}
原因分析:
当使用shell模块并且返回为空的时候,ansible就会认为shell脚本出错了,rc就返回1。
解决方案:
在shell命令末尾增加cat,将返回的内容通过管道传递给cat,使用cat返回的rc始终为0. 最好的解决方式,无论你要获取整个返回内容或者返回行数。
-
name: Check the weblogic without wc but use cat
shell: “ps -ef|grep weblogic|grep -v grep|cat”
register: check_weblogic1
ignore_errors: true -
name: print the check_weblogic1
debug:
msg: “{{ check_weblogic1 }}”
在shell命令末尾增加wc -l,计算返回的行数,保证shell返回始终不为空。 -
name: Check the weblogic with wc
shell: “ps -ef|grep weblogic|grep -v grep|wc -l”
register: check_weblogic2
ignore_errors: true -
name: print the check_weblogic2
debug:
msg: “{{ check_weblogic2.stdout|int }}”
在脚本最后面增加ignore_errors: true,最不推荐的方式,除非暂时没找到根本原因,应急。 -
name: Check the weblogic without wc
shell: “ps -ef|grep weblogic|grep -v grep”
register: check_weblogic0
ignore_errors: true
2.copy模块常见错误
2.1 使用copy模块,遇到Remote copy does not support recursive copy of directory
ansible all -m copy -a ‘src=/root/ansible/file1 dest=/etc/cc/file1 remote_src=yes backup=yes mode=0755’
TASK [cp files below folder4 to bak1] *************************************************************
ok: [localhost] => (item=subfile1)
ok: [localhost] => (item=subfile2)
failed: [localhost] (item=subfolder1) => {“changed”: false, “item”: “subfolder1”, “msg”: “Remote copy does not support recursive copy of directory: /apps/ansible-test/folder4/subfolder1”}
to retry, use: --limit @/apps/ansible-test/test-cp.retry
PLAY RECAP ****************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=1
原因分析:
如果在远程机器上执行copy,相当于在远端机器本机执行cp命令,remote_src: true。对于asible 2.6,只支持copy单个文件,不允许递归copy。对于ansible 2.8 已经支持递归复制。详见官方说明:https://docs.ansible.com/ansible/latest/modules/copy_module.html
解决方案:
使用ansible 2.8 或者 使用linux shell cp -rf实现递归复制。
ansible all -m shell -a ‘cp -rf /root/ansible/* /etc/cc/file1’
3.ansible Permission denied
3.1 ansible_remote_tmp 设置不合理导致任务无法继续执行
当远程机器的某个目录挂载了一个磁盘,而且设置了不允许执行文件,碰巧ansible_remote_tmp 指向这个目录,会导致某些ansible模块执行失败。
解决方案:
ansible_remote_tmp 避免使用默认值(~/.ansible/tmp ),并且指定一个足够大,并且有读写执行权限的目录。
ansible命令执行过程
1. 加载自己的配置文件 默认/etc/ansible/ansible.cfg
2. 加载自己对应的模块文件,如command
3. 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/xyz.py文件
4. 给文件+x执行
5. 执行并返回结果
6. 删除临时py文件,sleep 0退出