Issue
I'm on ansible-2.0.1.0-2.el7.noarch
(but tried with 1.9.4
as well) and I'm trying to run this playbook:
- hosts: all
remote_user: root
tasks:
- shell:
pgrep --full 'sleep' && pkill --full 'sleep' || true
but I'm getting:
# ansible-playbook -i aaa.ini aaa.yaml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [192.168.122.100]
TASK [command] *****************************************************************
fatal: [192.168.122.100]: FAILED! => {"changed": true, "cmd": "pgrep --full 'sleep' && pkill --full 'sleep' || true", "delta": "0:00:00.158772", "end": "2016-05-05 00:33:49.072139", "failed": true, "rc": -15, "start": "2016-05-05 00:33:48.913367", "stderr": "", "stdout": "385", "stdout_lines": ["385"], "warnings": []}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @aaa.retry
PLAY RECAP *********************************************************************
192.168.122.100 : ok=1 changed=0 unreachable=0 failed=1
When I run the command directly, it works:
host # ssh [email protected]
vm # pgrep --full 'sleep' && pkill --full 'sleep' || true
vm # echo $?
0
Please do you have any idea on what I'm doing wrong?
UPDATE as per @Dag's answer bellow which deciphered this for me:
$ ansible localhost -m shell -a "pgrep --list-full -f process_that_does_not_exists"
localhost | SUCCESS | rc=0 >>
828 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists
835 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists
836 /usr/bin/python /usr/bin/ansible localhost -m shell -a pgrep --list-full -f process_that_does_not_exists
so the command with && pkill ...
was actually killing some Ansible's processes. Looks like I have to add some more filtering to make this work safely.
Solution
What is very apparent from your output is that the command returns -15. Which means the process was terminated.
If I execute your command on localhost, I am actually killing my own ansible run.
[dag@moria ~]$ ansible localhost -m shell -a 'pgrep -f 'sleep' && pkill -f 'sleep' || true'
Terminated
So you are doing the same from a playbook, and are killing part of Ansible's processes, but not too many so it can properly report back some output and the return code. (I was actually killing the main calling process on my own system too !)
Answered By - Dag Wieers Answer Checked By - Willingham (WPSolving Volunteer)