ansible执行 grep报错

在Ansible中执行shell任务时遇到错误,报错信息显示'non-zero return code',rc为1,具体任务是通过ps命令查找apache进程但未使用grep -q选项,导致当grep找不到匹配结果时返回1,从而触发Ansible任务失败。解决方法是使用grep -q选项,该选项在找到匹配时立即退出,即使没有匹配也会返回0。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Background

ansible 执行 shell 报错:“msg”: “non-zero return code”, “rc”: 1

playbook 配置

- name: check apache httpd progress
  command: /bin/bash -c "ps -ef | grep apache | grep -v apache"
  register: ps_apache

报错信息
TASK [apache : check apache httpd progress] ********************************************************************************************************************************
fatal: [10.222.6.180]: FAILED! => {“changed”: true, “cmd”: ["/bin/bash", “-c”, “ps -ef | grep apache | grep -v apache”], “delta”: “0:00:00.021526”, “end”: “2019-12-25 11:19:16.191242”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-12-25 11:19:16.169716”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
fatal: [10.241.81.70]: FAILED! => {“changed”: true, “cmd”: ["/bin/bash", “-c”, “ps -ef | grep apache | grep -v apache”], “delta”: “0:00:00.015040”, “end”: “2019-12-25 11:19:16.289464”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-12-25 11:19:16.274424”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}

原因:grep 找不到结果时,会返回 1

$ man grep

EXIT STATUS
       Normally, the exit status is 0 if selected lines are found and 1 otherwise.  But the exit status is 2 if  an  error  occurred,  unless  the  -q  or
       --quiet  or  --silent  option  is  used and a selected line is found.  Note, however, that POSIX only mandates, for programs such as grep, cmp, and
       diff, that the exit status in case of error be greater than 1; it is therefore advisable, for the sake of portability, to use logic that tests  for
       this general condition instead of strict equality with 2.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值