Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart service when service file changes when using Ansible

I am creating a systemd service using template module

---
- name: Systemd service
  template:
    src: sonar.unit.j2
    dest: /etc/systemd/system/sonarqube.service
  when: "ansible_service_mgr == 'systemd'" 

The contents of the sonarqube.service can change of course. On change I want to restart the service. How can I do this?

like image 676
onknows Avatar asked Aug 20 '19 10:08

onknows


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 check my Ansible service status?

Just run the task service: name=httpd state=started with the option --check . This tells you, if the service needs to be started, which means that it is down. If the task shows no change, it is up already.

What is daemon reload in Ansible?

daemon_reload. aliases: daemon-reload. boolean. Run daemon-reload before doing any other operations, to make sure systemd has read any changes. When set to true , runs daemon-reload even if the module does not start or stop anything.

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.

When to use Ansible handlers?

Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified.

How do I use variables in Ansible?

You may want your Ansible handlers to use variables. For example, if the name of a service varies slightly by distribution, you want your output to show the exact name of the restarted service for each target machine. Avoid placing variables in the name of the handler.

Does Ansible report changed scripts as changed or not?

Consider you are running a command or shell module with some complex script or a simple command. Ansible would report it as changed as long as the command (or) script give ZERO return code. But how would you decide whether the command or script ran successfully? or met your needs?

Which Ansible services are supported on remote hosts?

Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart. For Windows targets, use the ansible.windows.win_service module instead. This module has a corresponding action plugin. Additional arguments provided on the command line.


Video Answer


2 Answers

There are two solutions.

Register + When changed

You can register template module output (with its status change),

register: service_conf

and then use when clause.

when: service_conf.changed

For example:

---
- name: Systemd service
  template:
    src: sonar.unit.j2
    dest: /etc/systemd/system/sonarqube.service
  when: "ansible_service_mgr == 'systemd'" 
  register: service_conf

- name: restart service
  service:
    name: sonarqube
    state: restarted
  when: service_conf.changed

Handler + Notify

You define your restart service task as handler. And then in your template task you notify the handler.

tasks:
  - name: Add Sonarqube to Systemd service
    template:
      src: sonar.unit.j2
      dest: /etc/systemd/system/sonarqube.service
    when: "ansible_service_mgr == 'systemd'"
    notify: Restart Sonarqube
  - …

handlers:
  - name: Restart Sonarqube
    service:
      name: sonarqube
      state: restarted

More info can be found in Ansible Doc.

Difference between those 2?

In the first case, the service will restart directly. In the case of the handler the restart will happen at the end of the play.

Another difference will be, if you have several tasks changes that need to restart of your service, you simply add the notify to all of them.

  • The handler will run if any of those task get a changed status. With the first solution, you will have to register several return. And it will generate a longer when clause_1 or clause_2 or
  • The handler will run only once even if notified several times.
like image 125
dubioushencho Avatar answered Sep 20 '22 06:09

dubioushencho


This calls for a handler

---
 - name: Testplaybook
   hosts: all
   handlers:
     - name: restart_service
       service:
         name: <servicename>
         state: restarted
   tasks:
     - template:
         src: ...
         dest: ...
       notify:
         - restart_service

The handler will automatically get notified by the module when something changed. See the documentatation for further information on handlers.

like image 40
dgw Avatar answered Sep 22 '22 06:09

dgw