user389955
user389955

Reputation: 10467

ansible shell: how to show grep result the same as run at local

- name: check process is running or not
  shell: ps -ef |grep abcd |grep defg
  register: result
  ignore_errors: yes
- debug: msg="{{ result.stdout }}"

the result shows:

TASK [debug] ***********
ok: [52.35.61.9] => {
"msg": "ec2-user 28932 28931  0 17:42 pts/0    00:00:00 /bin/sh -c ps 
-ef |grep abcd |grep defg"
}

but if I login to that machine and directly run:

ps -ef |grep abcd |grep defg, 

I see nothing because the process has stopped.

in ansible I need to check whether the process is already run. and then run it only if it is not. That is why I use shell and ps (command does not support pipe so I have to use shell) . but calling shell from ansible always shows sth. even the process is not run.

how to make it not show anything, same as when run ps locally?

Upvotes: 0

Views: 2098

Answers (1)

helloV
helloV

Reputation: 52393

Try enclosing a character in square brackets like this:

shell: ps -ef |grep [a]bcd |grep defg

This is just to make grep ignore that line in ps output.

Upvotes: 1

Related Questions