Verifique se existe serviço com Ansible

Eu tenho um manual Ansible para implantar um aplicativo Java como um daemon init.d.

Sendo iniciante no Ansible e no Linux, estou tendo problemas para executar tarefas condicionalmente em um host com base no status do host.

Nomeadamente, tenho alguns hosts com o serviço já presente e em execução, onde desejo interrompê-lo antes de fazer qualquer outra coisa. E pode haver novos hosts, que ainda não têm o serviço. Então não posso simplesmente usarservice: name={{service_name}} state=stopped, porque isso falhará em novos hosts.

Como posso conseguir isso? Aqui está o que eu tenho até agora:

  - name: Check if Service Exists
    shell: "if chkconfig --list | grep -q my_service;   then echo true;   else echo false; fi;"
    register: service_exists

# This should only execute on hosts where the service is present
  - name: Stop Service
    service: name={{service_name}} state=stopped
    when: service_exists
    register: service_stopped

# This too
  - name: Remove Old App Folder
    command: rm -rf {{app_target_folder}}
    when: service_exists

# This should be executed on all hosts, but only after the service has stopped, if it was present
  - name: Unpack App Archive
    unarchive: src=../target/{{app_tar_name}} dest=/opt

questionAnswers(5)

yourAnswerToTheQuestion