command ps -ef | grep failed in Ansible

7,157 views
Skip to first unread message

fanvalt

unread,
May 24, 2016, 6:50:11 AM5/24/16
to Ansible Project
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


fanvalt

unread,
May 24, 2016, 7:03:24 AM5/24/16
to Ansible Project
It seems that the multiple use of the pipe "|" is the issue.
How can I bypass this ?

Brian Coca

unread,
May 24, 2016, 10:06:14 AM5/24/16
to ansible...@googlegroups.com
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.


----------
Brian Coca

fanvalt

unread,
May 24, 2016, 10:24:05 AM5/24/16
to Ansible Project
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


fanvalt

unread,
May 24, 2016, 10:24:46 AM5/24/16
to Ansible Project


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 

Kai Stian Olstad

unread,
May 24, 2016, 10:33:38 AM5/24/16
to ansible...@googlegroups.com
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


--
Kai Stian Olstad

fanvalt

unread,
May 24, 2016, 10:40:14 AM5/24/16
to Ansible Project, ansible-pr...@olstad.com
ok thank you,

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

Will Thames

unread,
May 24, 2016, 9:08:46 PM5/24/16
to Ansible Project, ansible-pr...@olstad.com
Also, have a look at pgrep. You can probably write that command without a pipe using pgrep.

Wawrzek Niewodniczanski

unread,
Jun 8, 2016, 5:16:52 PM6/8/16
to ansible...@googlegroups.com
On 24 May 2016 at 11:50, fanvalt <fanny....@gmail.com> wrote:
> 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

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
--
Dr Wawrzyniec Niewodniczański or Wawrzek for short
PhD in Quantum Chemistry & MSc in Molecular Engineering
WWW: http://wawrzek.name E-MAIL: jo...@wawrzek.name
Linux User #177124
Reply all
Reply to author
Forward
0 new messages