1


I'm getting an unexpected behavior from executing a shell remotely with Ansible. When I execute the following command on the remote machine, the result is:

$ echo $(pgrep -f jboss)
13248

Now if I execute the command as Ansible shell I get as return multiple pids:

$ ansible jboss.servers -m shell -a 'echo $(pgrep -f jboss)' -u centos
10.3.9.155 | SUCCESS | rc=0 >>
13248 16362 16363

As I need to further elaborate the shell, I need to find a way to get the same result as if I'm on the remote machine. I've tried with:

'echo $(pgrep -f jboss | awk '{ print $1 }')'

But that doesn't work because the expression is already in single quotes. Any help ?

1 Answer 1

1

Because pgrep returns the Ansible processes too. Try:

ansible jboss.servers -m shell -a 'ps ax | grep jboss'

then you know why you are getting the additional PIDs. One way to fix it is not to use pgrep and instead use ps and grep.

ansible jboss.servers -m shell -a 'ps ax | grep [j]boss | cut -d " " -f2'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I had just to change the column to be f1 (otherwise the tty is returned). Now it works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.