Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Ansible from starting then restarting a service?

Tags:

ansible

In many of my Ansible roles I have a handler to restart a service and a task to make sure the service is enabled and started. When this is a first run Ansible will start my service (Ensure mongodb is started and enabled) and then at the end run the restart handler. How do I tell Ansible to only start it once.

Sample play:

---
- name: Install MongoDB package
  yum: name="mongodb-org-{{ mongodb_version }}" state=present

- name: Configure mongodb
  template: src=mongod.conf.j2 dest=/etc/{{ mongodb_config_file }} owner=root group=root mode=0644
  notify: mongodb restart

- name: Ensure mongodb is started and enabled
  service: name={{ mongodb_daemon_name }} state=started enabled=yes
like image 666
Ryan Currah Avatar asked Jun 15 '16 19:06

Ryan Currah


2 Answers

Insert a flush_handlers task just in the front of the server started task to run the restart handler. Like:

- name: flush handlers
  meta: flush_handlers

## your service start task
- name:  Ensure mongodb is started and enabled
  service:
    service: name={{ mongodb_daemon_name }} state=started enabled=yes

If handlers are notified, the handler will restart the services (I assume you have handlers that restart the services).

If handlers are not notified, the Ensure service started task will start the service if it is not already started.

like image 82
Ding-Yi Chen Avatar answered Oct 08 '22 05:10

Ding-Yi Chen


It's been a while since I asked this but anywho, if it helps, the solution I ended up with is below...

The trick is to...

  1. Have the service task in the tasks/main.yml file with the parameters state: started and enabled: yes split out into their own tasks
  2. For the Ensure mongodb is started task register a mongodb_service_started variable
  3. Use the newly registered variable mongodb_service_started in the when clause of the Restart mongodb handler to only run when the Ensure mongodb is started task did NOT change

This ensures that your application does not get started then subsequently restarted in the same play and prevents your application from being stopped when it is running some critical function on startup. For example database migrations. Restarting a service right after it's been started can cause an inconsistent state.

tasks/main.yml

---
- name: Install MongoDB package
  yum: 
    name: "mongodb-org-{{ mongodb_version }}" 
    state: present
  notify: Restart mongodb

- name: Configure mongodb
  template: 
    src: mongod.conf.j2 
    dest: "/etc/{{ mongodb_config_file }}"
    owner: root 
    group: root 
    mode: 0644
  notify: Restart mongodb

- name: Ensure mongodb is started
  service: 
    name: "{{ mongodb_daemon_name }}" 
    state: started
  register: mongodb_service_started

- name: Ensure mongodb is enabled
  service: 
    name: "{{ mongodb_daemon_name }}"
    enabled: yes

handlers/main.yml

---
- name: Restart mongodb
  service: 
    name: "{{ mongodb_daemon_name }}" 
    state: started
  when: mongodb_service_started.changed == False
like image 26
Ryan Currah Avatar answered Oct 08 '22 05:10

Ryan Currah