本文共 3135 字,大约阅读时间需要 10 分钟。
由于工作中经常用到ansible,所以整理了常用的ansible错误及原因分析,方便自己也方便别人参考。
- name: Check the weblogic without wc shell: "ps -ef|grep weblogic|grep -v grep" register: check_weblogic0 ignore_errors: true
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": []}...ignoringok: [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
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.retryPLAY 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'
转载地址:http://scktb.baihongyu.com/