Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if service exists with Ansible

I have an Ansible playbook for deploying a Java app as an init.d daemon.

Being a beginner in both Ansible and Linux I'm having trouble to conditionally execute tasks on a host based on the host's status.

Namely I have some hosts having the service already present and running where I want to stop it before doing anything else. And then there might be new hosts, which don't have the service yet. So I can't simply use service: name={{service_name}} state=stopped, because this will fail on new hosts.

How I can I achieve this? Here's what I have so far:

  - 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 
like image 208
EagleBeak Avatar asked May 19 '15 14:05

EagleBeak


People also ask

What is service module in ansible?

What does the Ansible service module do? Ansible's service module controls services on remote hosts and is useful for these common tasks: Start, stop or restart a service on a remote host.

How do I start ansible service?

Use systemctl restart ansible-tower to restart services on clustered environments instead. Also you must restart each cluster node for certain changes to persist as opposed to a single node for a localhost install.

Is ansible a service?

Ansible® is an open source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more. Ansible's main strengths are simplicity and ease of use.

What is regex in ansible?

regex - Extracting part of the string using Ansible regex_search and save the output as a variable - Server Fault.


2 Answers

See the service_facts module, new in Ansible 2.5.

- name: Populate service facts   service_facts: - debug:     msg: Docker installed!   when: "'docker' in services" 
like image 102
Aidan Feldman Avatar answered Sep 20 '22 11:09

Aidan Feldman


Of course I could also just check if the wrapper script exists in /etc/init.d. So this is what I ended up with:

  - name: Check if Service Exists     stat: path=/etc/init.d/{{service_name}}     register: service_status    - name: Stop Service     service: name={{service_name}} state=stopped     when: service_status.stat.exists     register: service_stopped 
like image 30
EagleBeak Avatar answered Sep 18 '22 11:09

EagleBeak