Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I restart a service using ansible?

Tags:

ansible

I tried to use Ansible service module to restart a service but I got an error.

 tasks:
    - ini_file: dest=/etc/dd-agent/datadog.conf
               section=Main
               option=use_mount
               state=absent
      register: ddagent
    - service: name='datadog-agent' state=reloaded
      when: ddagent.changed

This generated this error: ERROR: change handler (restart datadog) is not defined

I know that an alternative is to execute:

- command: "service datadog-agent restart"

Still, in this case what's the purpose of the service module?

like image 830
sorin Avatar asked Jan 19 '16 17:01

sorin


People also ask

How do I restart 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.

How do I start and stop a service in Ansible?

Take this code: - hosts: localhost become: yes tasks: - name: Stop and Start ssh service: name: ssh state: stopped and check the status using "service ssh status" .

How do you stop Ansible service?

How to stop a service. Set the name parameter to the service name and the state parameter to stopped to stop a service. If the service is not running, Ansible will do nothing.

How do you check if a service is running using Ansible?

If you're checking services, you can do it the Ansible way by running the service_facts module and then checking ansible_local. services["apache2"]. status as well.


1 Answers

You should add the following code:

handlers:
  - name: restart datadog
    service: name=datadog-agent state=restarted

The problem that you are facing is that you don't have the handler defined. This will do the job

like image 179
Javier Segura Avatar answered Sep 28 '22 08:09

Javier Segura