command ps -ef | grep failed in Ansible

Hello,

I want to check if a process to run on my server by using the ps command:

  • name: Test if openhr runs
    shell: ps -ef | grep -v grep | grep {{ ansible_user }} | grep dispatcher | grep {{ rep_user }}/{{ openhrname }}/bin
    register: procopenhr

I receive the following error message, I cannot figure out what’s wrong with my command:

TASK [openhr : Test if openhr runs] ******************************************
fatal: [localhost]: FAILED! => {“changed”: true, “cmd”: “ps -ef | grep -v grep | grep fval | grep dispatcher | grep home/fval/openhr/bin”, “delta”: “0:00:00.011877”, “end”: “2016-05-24 12:36:58.184606”, “failed”: true, “rc”: 1, “start”: “2016-05-24 12:36:58.172729”, “stderr”: “”, “stdout”: “”, “stdout_lines”: , “warnings”: }

I tried with quoting the last grep, without success: “grep {{ rep_user }}/{{ openhrname }}/bin”

What is wrong ?

Regards

It seems that the multiple use of the pipe “|” is the issue.
How can I bypass this ?

use shell module, command does not allow for pipes nor redirection as it does not invoke a shell direclty, shell module DOES invoke a shell.

Thank you but I do use the shell module:

  • name: Test if openhr runs
    shell: ps -ef | grep -v grep | grep {{ ansible_user }} | grep dispatcher | grep {{ rep_user }}/{{ openhrname }}/bin
    register: procopenhr

Thank you but I do use the shell module:

  • name: Test if openhr runs
    shell: ps -ef | grep -v grep | grep {{ ansible_user }} | grep dispatcher | grep {{ rep_user }}/{{ openhrname }}/bin
    register: procopenhr

I did test, a pipe is allowed, with 2 pipes the error does occur

When grep doesn't find the string it will exit with 1.
Ansible threat this("rc": 1) as a failure, you can change this behaviour with ignore_errors, failed_when or changed_when depending of what you try to achieve.

You can read more about it here:
https://docs.ansible.com/ansible/playbooks_error_handling.html

ok thank you,

I will try to find a way to use the when condition then.

Also, have a look at pgrep. You can probably write that command without a pipe using pgrep.

Aside Ansible you can probably simply your command. I think following
should work

ps -u {{ ansible_user }} | grep [d]ispatcher | grep {{ rep_user }}/{{
openhrname }}/bin

I wonder if the second grep is actual necessary.

Wawrzek