Madoc Comadrin
Madoc Comadrin

Reputation: 568

Using Ansible to stop service that might not exist

I am using Ansible 2.6.1.

I am trying to ensure that certain service is not running on target hosts. Problem is that the service might not exist at all on some hosts. If this is the case Ansible fails with error because of missing service. Services are run by Systemd.

Using service module:

  - name: Stop service
    service:
      name: '{{ target_service }}'
      state: stopped

Fails with error Could not find the requested service SERVICE: host

Trying with command module:

 - name: Stop service
   command: service {{ target_service }} stop

Gives error: Failed to stop SERVICE.service: Unit SERVICE.service not loaded.

I know I could use ignore_errors: yes but it might hide real errors too.

An other solution would be having 2 tasks. One checking for existance of service and other that is run only when first task found service but feels complex.

Is there simpler way to ensure that service is stopped and avoid errors if the service does not exists?

Upvotes: 15

Views: 15796

Answers (6)

Roy
Roy

Reputation: 123

Same solution as @ToughKernel's but use systemd to manage service.

- name: disable ntpd service
  systemd:
    name: ntpd
    enabled: no
    state: stopped
  register: stop_service
  failed_when:
    - stop_service.failed == true
    - '"Could not find the requested service" not in stop_service.msg'
    # the order is important, only failed == true, there will be
    # attribute 'msg' in the result

Upvotes: 12

Isac Casapu
Isac Casapu

Reputation: 1301

When the service module fails, check if the service that needs to be stopped is installed at all. That is similar to this answer, but avoids the rather lengthy gathering of service facts unless necessary.

- name: Stop a service
  block:
    - name: Attempt to stop the service
      service:
        name:  < service name >
        state: stopped
  rescue:
    - name: Get the list of services
      service_facts:

    - name: Verify that Nagios is not installed
      assert:
        that:
          - "'< service name >.service' not in services"

Upvotes: 0

ToughKernel
ToughKernel

Reputation: 186

The following will register the module output in service_stop; if the module execution's standard output does not contain "Could not find the requested service" AND the service fails to stop based on return code, the module execution will fail. Since you did not include the entire stack trace I am assuming the error you posted is in the standard output, you may need to change slightly based on your error.

- name: Stop service
  register: service_stop
  failed_when: 
    - '"Could not find the requested service" not in service_stop.stdout'
    - service_stop.rc != 0
  service:
    name: '{{ target_service }}'
    state: stopped

Upvotes: 7

Nazar
Nazar

Reputation: 704

I'm using the following steps:

- name: Get the list of services
  service_facts:

- name: Stop service
  systemd:
    name: <service_name_here>
    state: stopped
  when: "'<service_name_here>.service' in services"

service_facts could be called once in the gathering facts phase.

Upvotes: 10

Same solution as Vladimir's, but for Ubuntu (systemd) and with better state handling:

- name: restart {{ target_service }} if exists
  shell: if systemctl is-enabled --quiet {{ target_service }}; then systemctl restart {{ target_service }} && echo restarted ; fi
  register: output
  changed_when: "'restarted' in output.stdout"

It produces 3 states:

  • service is absent or disabled — ok
  • service exists and was restarted — changed
  • service exists and restart failed — failed

Upvotes: 2

Vladimir Botka
Vladimir Botka

Reputation: 68034

IMHO there isn't simpler way to ensure that service is stopped. Ansible service module doesn't check service's existence. Either (1) more then one task, or (2) command that check service's existence is needed. The command would be OS specific. For example for FreeBSD

command: "service -e | grep {{ target_service }} && service {{ target_service }} stop"

Upvotes: 2

Related Questions