背景

由于工作中经常用到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. Adapter之spinner

    前言: 在写代码当中有时候会用到下拉列表,下面我们讲一下spinner 正文: 因为比较简单,和之前的listView很像,所以直接上代码 <Spinner android:layout_wid ...

  2. git使用问题二删除远程仓库文件,本地保留不动

    git rm --cached filename/-r directory git commit "xxxx" git push

  3. win7 & win10 安装AD管理工具

    总所周知,AD域的作用对于一个公司有着无比重要的作用,但是在Win7/10系统下该如何去管理AD域呢. 对于AD域的服务器搭建,在这里我们不进行说明,感兴趣的同学可以去Google相关的资料,现在主要 ...

  4. 004.Oracle数据库 , 查询多字段连接合并

    /*Oracle数据库查询日期在两者之间*/ SELECT PKID , OCCUR_DATE, PKID || TO_CHAR( OCCUR_DATE, ' yyyy/mm/dd hh24:mi:s ...

  5. mysql使用的坑

    一: mysql默认是安装记录的物理顺序取数据的,如果不加order by 排序,可能得不到预期的结果. (1) 获取 两个时间点的 id  (很快) $sql = ‘select id from a ...

  6. Linux间传输文件 scp

    scp scp使用ssh来传输数据,使用相同的认证方式,所以配置好ssh后,根据用户名和密码来读写远程文件.基本命令如下,输完命令,回车,输入远程用户对应的密码: 从本机复制到远程: 文件:scp F ...

  7. 记号一下用iptables做的端口转发

    iptables -t nat -I PREROUTING -p tcp -m tcp --dport 83 -j DNAT --to-destination 192.168.1.55:443ipta ...

  8. 51nod 1559 车和矩形

    http://www.51nod.com/Challenge/Problem.html#problemId=1559 倘若矩形是受保护的,那么矩形内每一行至少有一个车或者每一列至少有一个车 判断矩形内 ...

  9. 剑指offer系列(六)

    题目描述: 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList,例如按照链表顺序,1->2->3->4->5->6->7->8,那么我们将得到{8 ...

  10. Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in

    原语句: db.carMongoDTO.aggregate({}}}, {}}}) 报错: Exceeded memory limit for $group, but didn't allow ext ...