2

I wanted to run a command in shell (basically via ansible playbook), the condition is, if the ps status gives non zero exit code then it needs to run another command. I don't wanted to split this into two tasks, one to check the ps status and another to execute the command. Instead using if/else condition, can this be merged into a single task?

ps -ef | grep -v grep | grep httpd

If the above command exits with non zero status, then I need to fire another command which is given below. How can I merge these two commands as one and include this as a single task?

nohup /root/scripts/httpd.sh start &
| improve this question | | | | |
2

Solution

This does what you want:

  tasks:
    - name: pser
      shell: ps -ef | grep [h]ttpd && echo found || nohup sleep 1000 &

NOTE: I'm using the command sleep 1000 as a stand-in for your Apache daemon process, httpd.

Example Run

$ cat pb.yml
- name: Hello Ansible - quick start
  hosts: all
  connection: local

  tasks:
    - name: pser
      shell: ps -ef | grep [h]ttpd && echo found || nohup sleep 1000 &

Notice we have neither a sleep or httpd process present:

$ ps -eaf|grep -E "[s]leep|[h]ttpd"
$

Now we run the playbook:

$ ansible-playbook -i inventory pb.yml

PLAY [Hello Ansible - quick start] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [pser] ************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

Now we have a sleep process going:

$ ps -eaf|grep -E "[s]leep|[h]ttpd"
  501 34422     1   0  2:00AM ttys000    0:00.00 /bin/bash -c ps -ef | grep [h]ttpd && echo found || nohup sleep 1000 &
  501 34425 34422   0  2:00AM ttys000    0:00.00 sleep 1000

Now if we reset things and do the same thing but this time open up a edit window with a filename of httpd to "fake out" the grep [h]ttpd:

$ ps -eaf|grep -E "[s]leep|[h]ttpd"
  501 34542     1   0  2:02AM ??         0:00.27 /usr/local/Cellar/macvim/HEAD-4e631a0/MacVim.app/Contents/bin/../MacOS/Vim -f -g httpd

Running the playbook this time will do nothing:

$ ansible-playbook -i inventory pb.yml

PLAY [Hello Ansible - quick start] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [pser] ************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

Still just our edit window with the 'httpd' file open:

$ ps -eaf|grep -E "[s]leep|[h]ttpd"
  501 34542     1   0  2:02AM ??         0:00.27 /usr/local/Cellar/macvim/HEAD-4e631a0/MacVim.app/Contents/bin/../MacOS/Vim -f -g httpd
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.