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