I have two bellow tasks as part from my playbook
- name: "Verify httpd.service no running on node {{ ansible_hostname }}"
shell: "ps -ef | grep httpd"
register: _ps_httpd
become: true
- name: stop httpd is exit code eq to 0
shell: "kill -9 $(ps -ef | grep httpd| awk '{print $2}')"
when: _ps_httpd.rc == 0
become: true
ignore_errors: true
This two tasks that should force kill process. Currently I'm getting the error
"msg": "non-zero return code", "rc": -9
What do I miss here? Any idea how to get resolve this?
In addition, I would like to add the option exit from ansible-playbook
run if not successful.
Best Answer
To achieve the goal in Ansible it is recommended in general to use service modules,
service
,sysvinit
orsystemd
. In example likeIf you like to use the
shell
_module, for me more work was necessary.In example for
nginx
get the right PID first, since there are a primary and four worker processes.It would also be possible to do something like
Regarding
to end the playbook run you could use
use
fail
_module toor the
assert
_module.Regarding the exit code (EC) or return code (RC) you may have a look into How do I get the list of exit codes (and/or return codes) and meaning for a command/utility?.