Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible handler runs only if changed: true

Installing ntp with Ansible, I notify handler in order to start ntpd service:
Task:

---
# roles/common/tasks/ntp.yml
  - name: ntp | installing
    yum: name=ntp state=latest
    notify: start ntp

Handler:

---
# roles/common/handlers/main.yml

- name: start ntp
  service: name=ntpd state=started

If service has not been installed, ansible installs and starts it.
If service has been installed already, but is not running, it does not notify handler:
status of task is changed: false
That means, I cannot start it, if it has been already presented in OS.

Is there any good practice that helps to be sure that service has been installed and is in running state?

PS: I may do so:

---
# roles/common/tasks/ntp.yml
  - name: ntp | installing
    yum: name=ntp state=latest
    notify: start ntp
    changed: true

but I am not sure that it is good practice.

like image 615
antonbormotov Avatar asked Feb 24 '16 10:02

antonbormotov


People also ask

How does handler work in Ansible?

In a nutshell, handlers are special tasks that only get executed when triggered via the notify directive. Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.

What is difference between tasks and handlers in Ansible?

Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify keyword and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.

What is the use of notify and handlers in Ansible?

In Ansible, a handler refers to a particular task that executes when triggered by the notify module. Handlers perform an action defined in the task when a change occurs in the remote host. Handlers are helpful when you need to perform a task that relies on a specific task's success or failure.

What's the Ansible playbook execution order?

Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.


2 Answers

From the Intro to Playbooks guide:

As we’ve mentioned, modules are written to be ‘idempotent’ and can relay when they have made a change on the remote system. Playbooks recognize this and have a basic event system that can be used to respond to change.

These ‘notify’ actions are triggered at the end of each block of tasks in a playbook, and will only be triggered once even if notified by multiple different tasks.

Handlers only run on change by design. If you change a configuration you often need to restart a service, but don't want to if nothing has changed.

What you want is to start a service if it is not already running. To do this you should use a regular task as described by @udondan :

- name: ntp | installing
  yum:
    name: ntp
    state: latest

- name: ntp | starting
  service:
    name: ntpd
    state: started
    enabled: yes

Ansible is idempotent by design, so this second task will only run if ntp is not already running. The enabled line will set the service to start on boot. Remove this line if that is not desired behavior.

like image 100
MillerGeek Avatar answered Sep 22 '22 04:09

MillerGeek


Why don't you just add a service task then? A handler usually is for restarting a service after configuration has changed. To ensure a service is running not matter what, just add at task like that:

- name: Ensure ntp is running
  service:
    name: ntpd
    state: started
like image 24
udondan Avatar answered Sep 22 '22 04:09

udondan