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.