博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ansible常见错误解析(停止更新)
阅读量:2353 次
发布时间:2019-05-10

本文共 3135 字,大约阅读时间需要 10 分钟。

最新的请访问: 

背景

由于工作中经常用到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": []}...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

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.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/

你可能感兴趣的文章
Vue.js入门学习(五)方法与事件处理器、表单控件绑定
查看>>
项目:Vue.js高仿饿了吗外卖APP(一)
查看>>
javascript中一些相对位置
查看>>
vue高仿饿了么课程项目--布局篇学习笔记
查看>>
es6 javascript的Iterator 和 for...of 循环
查看>>
Javascript中的shift() 、unshift() 和 pop()、push()区别
查看>>
将嵌套的数组扁平化
查看>>
vue-router的两种模式及区别
查看>>
c中嵌入python
查看>>
python的C扩展
查看>>
python的USB通信
查看>>
eclipse svn
查看>>
SPSS基础教程:SPSS统计分析基础
查看>>
IBM SPSS Modeler 客户端 vs 服务器的区别详解
查看>>
DataStage On Cloud,构建云端的企业数据集成平台
查看>>
ICMP协议
查看>>
SSL协议
查看>>
IMAP,POP3,SMTP协议
查看>>
数据库协议
查看>>
SNMP协议
查看>>