背景

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

最新文章

  1. MySQL线程池
  2. javaScript获取url中的参数
  3. 041. asp.net中内容页访问母版页中的控件
  4. LinkedHahsMap和HashMap的比较
  5. 深入理解Java:注解(Annotation)--注解处理器
  6. Sql Server按树形结构排序查询表记录
  7. 3.C#中泛型类的进一步探讨
  8. linux exec用法总结
  9. WebView(网络视图)的两种使用方式
  10. Yii处理流程
  11. Linux 下挂载硬盘的 方法
  12. Ubuntu抛弃了Untiy转向Gnome,美化之路怎么办?不用怕咱一步一步大变身!
  13. Vue2.x中的Render函数
  14. JavaWeb 后端 <十一> 之 DBUtils 框架 (基本使用 结果集 事务处理 对表读取)
  15. JS的this总结(上)-call()和apply()
  16. Neutron local network 学习
  17. 在layui中使用ajax不起作用
  18. (转)Understanding Memory in Deep Learning Systems: The Neuroscience, Psychology and Technology Perspectives
  19. PHP RBAC权限管理 基于角色的访问控制演示
  20. zabbix的日常监控-API

热门文章

  1. LR_问题_平均响应时间解释,summary与analysis不一致----Summary Report中的时间说明
  2. MQTT 协议学习:002- 通信报文的构成
  3. 快速为Eclipse配置PyDev插件
  4. springboot整合quartz并持久化到数据库
  5. php+ajax 实现无限树列表
  6. java 接口 2.19
  7. oracle job不运行,定位问题
  8. weblogic-开发模式与生产模式互换
  9. 配置anaconda 的仓库镜像
  10. C语言小游戏: 2048.c