TransWikia.com

Ansible - Handler not found on first attempt

DevOps Asked by Eric Saindon on August 22, 2021

Command run: sudo ansible-playbook site.yml --connection=local

Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:        18.04
Codename:       bionic

ansible 2.5.5

I’m encountering this error only on my first run through after I change the following variable in the following file:

./group_vars/all

ntpserver: pool.ntp.org

TASK [common : Configure NTP file] **************************************************************************************************************
ERROR! The requested handler 'restart ntp' was not found in either the main handlers list nor in the listening handlers list

On my second run through, I’m having no issues:

TASK [common : Configure NTP file] **************************************************************************************************************
ok: [127.0.0.1] => {"changed": false, "checksum": "cc9e1a2d8777588fa4675e52f7ca7e352b23188a", "dest": "/etc/ntp.conf", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/etc/ntp.conf", "size": 139, "state": "file", "uid": 0}

The folder and file structure I’m using is as follows:

https://github.com/ansible/ansible-examples/tree/master/lamp_simple_rhel7

./roles/common/handlers

- name: Restart NTP
  service: name=ntpd state=restarted`

./roles/common/tasks

---
- name: Install NTP
  apt: name=ntp state=present
  tags: ntp

- name: Configure NTP file
  template: src=ntp.conf.j2 dest=/etc/ntp.conf
  tags: ntp
  notify: restart ntp

- name: Install common packages
  apt: name={{ item }} state=present
  with_items:
    - fail2ban
    - ranger

- name: Start the NTP service
  service: name=ntp state=started enabled=yes
  tags: ntp

- name: Upgrade all packages to the latest version
  become: true
  apt:
    upgrade: yes
    update_cache: yes
    cache_valid_time: 86400 # one-day

3 Answers

I just found out the hard way. handlers only works with include_tasks not import_tasks .

The error message is rather missdirecting. I went all in adjusting indentation, syntax check and all . Finally I recreate the whole playbook but still fail. Finally switch to include_tasks then it works.

Rgrds

Answered by Jason on August 22, 2021

Ansible is case sensitive, hence a != A

Therefore, the correct code should look like this:

- name: Configure NTP file
  template: src=ntp.conf.j2 dest=/etc/ntp.conf
  tags: ntp
  notify: Restart NTP

Answered by Max on August 22, 2021

Name of the handler "Restart NTP" does not fit the handler notified "restart ntp". This is the reason of the error. On the second run the configuration file /etc/ntp.conf did not change ("changed": false). Therefor the handler was not notified, was not looked for and no error was reported.

Answered by Vladimir Botka on August 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP