# command ps -ef | grep failed in Ansible

**URL:** <https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537>\
**Category:** Ansible Project\
**Created:** [May 24, 2016, 10:50am UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537 "2016-05-24T10:50:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 24, 2016, 10:50am UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/1 "2016-05-24T10:50:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 24, 2016, 11:03am UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/2 "2016-05-24T11:03:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 24, 2016, 2:06pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/3 "2016-05-24T14:06:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 24, 2016, 2:24pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/4 "2016-05-24T14:24:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 24, 2016, 2:24pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/5 "2016-05-24T14:24:46Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 24, 2016, 2:33pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/6 "2016-05-24T14:33:13Z")

</div>

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](https://docs.ansible.com/ansible/playbooks_error_handling.html)

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 24, 2016, 2:40pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/7 "2016-05-24T14:40:14Z")

</div>

ok thank you,

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

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [May 25, 2016, 1:08am UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/8 "2016-05-25T01:08:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [June 8, 2016, 9:16pm UTC](https://forum.ansible.com/t/command-ps-ef-grep-failed-in-ansible/21537/9 "2016-06-08T21:16:25Z")

</div>

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
