背景

由于工作中经常用到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'

Ansible常见错误解析的更多相关文章

  1. Essay写作常见错误精选

    Essay写作常见错误精选.Essay写作有许多不为人注意的小细节,如果申请人在这些细节上不注意,往往会犯一些很典型的错误.和小编一起来看看留学Essay写作常见错误解析. 1)直接把申请学校A的Es ...

  2. Servlet的Service方法和doget 和 dopost方法的区别,常见的错误解析

    package com.sxt.in; import java.io.IOException; import javax.servlet.ServletException; import javax. ...

  3. coreseek常见错误原因及解决方法

    coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...

  4. .Net常见错误

    常见错误 #1: 把引用当做值来用,或者反过来 C++ 和其他很多语言的程序员,习惯了给变量赋值的时候,要么赋单纯的值,要么是现有对象的引用.然而,在C# 中,是值还是引用,是由写这个对象的程序员决定 ...

  5. WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭

    在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于WCF服务的特殊性,调试起来也不是那么方便,因此往往会花费不少时间来进行跟 ...

  6. Python程序的常见错误(收集篇)

    关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...

  7. 配置sql server 2000以允许远程访问 及 连接中的四个最常见错误

    地址:http://www.cnblogs.com/JoshuaDreaming/archive/2010/12/01/1893242.html 配置sql server 2000以允许远程访问适合故 ...

  8. java常见错误的列表

    ava常见错误列表: 找不到符号(symbol) 类X是public的,应该被声明在名为X.java的文件中 缺失类.接口或枚举类型 缺失X 缺失标识符 非法的表达式开头 类型不兼容 非法的方法声明; ...

  9. Python:常见错误集锦(持续更新ing)

    初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...

随机推荐

  1. Golang函数-递归函数

    Golang函数-递归函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  2. CodeForces - 862C Mahmoud and Ehab and the xor(构造)

    题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x. 分析: 1.因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ...

  3. wpf和winform的区别

    深入浅出WPF(7)——数据的绿色通道,Binding(上) 水之真谛关注6人评论28117人阅读2008-06-23 02:40:00  http://liuteimeng.blog.51cto.c ...

  4. vue - @click 传参删除

    <template>   <div id="app">      <div v-for="todo in  todos" :key ...

  5. git 创建分支并提交代码

    1.查看所有分支 git branch -a 2.查看当前分支 git branch 3.新建一个分支 git branch feature-xx 4.切换到新建分支上面 git checkout f ...

  6. leetcode1302 Deepest Leaves Sum

    """ Given a binary tree, return the sum of values of its deepest leaves. Example 1: I ...

  7. 十六、React 渲染数据注意事项、以及react-router4.x中使用js跳转路由(登录成功自动跳转首页)

    一.React加载数据流程回顾 先看上一节的产品详情代码:https://blog.csdn.net/u010132177/article/details/103184176 [Pcontent.js ...

  8. 08 SSM整合案例(企业权限管理系统):05.SSM整合案例的基本介绍

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 05.SSM整合案例的基本介绍 ...

  9. redis改配置

    命令行: 暂时生效,适合于做测试,或者线上马上服务修改,重启失效 CONFIG set stop-writes-on-bgsave-error no OK CONFIG get stop-writes ...

  10. Web基础之Spring AOP与事务

    Spring之AOP AOP 全程Aspect Oriented Programming,直译就是面向切面编程.和POP.OOP相似,它也是一种编程思想.OOP强调的是封装.继承.多态,也就是功能的模 ...