Solução
Isso faz o que você quer:
tasks:
- name: pser
shell: ps -ef | grep [h]ttpd && echo found || nohup sleep 1000 &
OBSERVAÇÃO: estou usando o comando sleep 1000
como um substituto para o seu processo de daemon do Apache, httpd
.
Exemplo de execução
$ 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 &
Observe que não temos um processo de suspensão ou httpd presente:
$ ps -eaf|grep -E "[s]leep|[h]ttpd"
$
Agora executamos o manual:
$ 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
Agora temos um processo de suspensão:
$ 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
Agora, se redefinirmos as coisas e fizermos a mesma coisa, desta vez, abra uma janela de edição com um nome de arquivo de httpd
para "falsificar" a 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
Executar o playbook desta vez não fará nada:
$ 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
Ainda é apenas a nossa janela de edição com o arquivo 'httpd' aberto:
$ 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