Ansible常见错误解析
背景
由于工作中经常用到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常见错误解析的更多相关文章
- Essay写作常见错误精选
Essay写作常见错误精选.Essay写作有许多不为人注意的小细节,如果申请人在这些细节上不注意,往往会犯一些很典型的错误.和小编一起来看看留学Essay写作常见错误解析. 1)直接把申请学校A的Es ...
- Servlet的Service方法和doget 和 dopost方法的区别,常见的错误解析
package com.sxt.in; import java.io.IOException; import javax.servlet.ServletException; import javax. ...
- coreseek常见错误原因及解决方法
coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...
- .Net常见错误
常见错误 #1: 把引用当做值来用,或者反过来 C++ 和其他很多语言的程序员,习惯了给变量赋值的时候,要么赋单纯的值,要么是现有对象的引用.然而,在C# 中,是值还是引用,是由写这个对象的程序员决定 ...
- WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭
在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于WCF服务的特殊性,调试起来也不是那么方便,因此往往会花费不少时间来进行跟 ...
- Python程序的常见错误(收集篇)
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...
- 配置sql server 2000以允许远程访问 及 连接中的四个最常见错误
地址:http://www.cnblogs.com/JoshuaDreaming/archive/2010/12/01/1893242.html 配置sql server 2000以允许远程访问适合故 ...
- java常见错误的列表
ava常见错误列表: 找不到符号(symbol) 类X是public的,应该被声明在名为X.java的文件中 缺失类.接口或枚举类型 缺失X 缺失标识符 非法的表达式开头 类型不兼容 非法的方法声明; ...
- Python:常见错误集锦(持续更新ing)
初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...
随机推荐
- k8s node断电重启
kubernetes断电重启 导致部分pod无法删除 dashboard上处于黄色 kubectl get处于terminate 状态 kubectl delete报错: An error occur ...
- 新闻网大数据实时分析可视化系统项目——19、Spark Streaming实时数据分析
1.Spark Streaming功能介绍 1)定义 Spark Streaming is an extension of the core Spark API that enables scalab ...
- vue - @click 传参删除
<template> <div id="app"> <div v-for="todo in todos" :key ...
- CSS - flex使行内元素快速对齐
div{ display:flex; alian-items:center; //使垂直对齐 justify-content:center //使水平对齐 }
- CVE-2019-0708—微软RDP远程桌面代码执行漏洞复现
0x01 2019年9月7日凌晨,msf上更新了0708的漏洞利用程序. 顿时安全群和朋友圈就爆炸了 - 奈何接到HW攻击队任务,又在家过了个中秋,0708才在今天更新. 0x02 环境 Window ...
- 向量容器vector操作
1.向量容器vector 1.1 vector说明 进行vector操作前应添加头文件#include<vector>: vector是向量类型,可以容纳许多类型的数据,因此也被称为容器: ...
- 054、Java中使用for循环第二种写法实现1~100的累加
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Codeforces 556A:Case of the Zeros and Ones
A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...
- 51nod 1515:明辨是非 并查集合并
1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给n组操作,每组操作形式为x y p. 当p为1时,如果第x ...
- 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 ...