# Conditional execution on hosts where service exists

**URL:** <https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259>\
**Category:** Ansible Project\
**Created:** [May 19, 2015, 2:49pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259 "2015-05-19T14:49:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander.konn](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alexander.konn](https://forum.ansible.com/u/alexander.konn)\
**Post date:** [May 19, 2015, 2:49pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259/1 "2015-05-19T14:49:05Z")

</div>

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:

```auto
  - 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

```

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [May 19, 2015, 2:56pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259/2 "2015-05-19T14:56:03Z")

</div>

Replying on phone so will keep it short.

Use ignore errors to allow the stop service team to fail without stopping the play

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 19, 2015, 2:56pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259/3 "2015-05-19T14:56:05Z")

</div>

you can:

- stat: /etc/init.d/my\_service  
&nbsp;&nbsp;register: my\_service

and now make other tasks conditional on :

&nbsp;&nbsp;when: my\_service|exists

---

<div class="post-metadata">

**Author:** ![alexander.konn](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alexander.konn](https://forum.ansible.com/u/alexander.konn)\
**Post date:** [May 19, 2015, 3:34pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259/4 "2015-05-19T15:34:13Z")

</div>

Great! Thanks.

This is what I ended up with:

```auto
- 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

```

---

<div class="post-metadata">

**Author:** ![Tom\_Cameron](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tom\_Cameron](https://forum.ansible.com/u/Tom_Cameron)\
**Post date:** [July 1, 2015, 8:24pm UTC](https://forum.ansible.com/t/conditional-execution-on-hosts-where-service-exists/18259/5 "2015-07-01T20:24:28Z")

</div>

Sorry for the super late reply to this, but you’ll miss services not found in the init.d directory. For example, this would fail on systemd, upstart, runit, and so on. It would also be platform specific to Linux. These may not be a big deal for the original author, but it should be noted for posterity’s sake.

Tom
