Conditional execution on hosts where service exists

7,149 views
Skip to first unread message

alexand...@gmail.com

unread,
May 19, 2015, 10:53:07 AM5/19/15
to ansible...@googlegroups.com

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

Stephen Granger

unread,
May 19, 2015, 10:56:06 AM5/19/15
to ansible...@googlegroups.com

Replying on phone so will keep it short.

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4bbba63e-050c-4a5b-9758-a4f23389cd2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
May 19, 2015, 10:56:11 AM5/19/15
to ansible...@googlegroups.com
you can:

- stat: /etc/init.d/my_service
register: my_service

and now make other tasks conditional on :

when: my_service|exists


--
Brian Coca

alexand...@gmail.com

unread,
May 19, 2015, 11:34:13 AM5/19/15
to ansible...@googlegroups.com
Great! Thanks.

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

Tom Cameron

unread,
Jul 1, 2015, 4:26:19 PM7/1/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages