Answers: <p>The name of the handler &quot;Restart NTP&quot; does not match the handler notified &quot;restart ntp&quot;. This is the reason of the error.</p>
  <p>On the second run the configuration file /etc/ntp.conf did not change (&quot;changed&quot;: false). Therefor the handler was not notified, was not looked for, and no error was reported.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-06-23T17:43:36.927Z
Answers: 2023-03-17T16:04:37.687Z
Answers: 2023-03-17T16:04:37.687Z
Answers: Vladimir Botka
Answers: <p>I just found out the hard way. handlers only works with include_tasks not import_tasks .</p>
  <p>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.</p>
  <p>Rgrds</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-17T01:32:17.277Z
Answers: 2020-07-17T01:32:17.277Z
Answers: Jason
Answers: <p>Ansible is case sensitive, hence a != A</p>
  
  <p>Therefore, the correct code should look like this:</p>
  
  <pre><code>- name: Configure NTP file
    template: src=ntp.conf.j2 dest=/etc/ntp.conf
    tags: ntp
    notify: Restart NTP
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-04-12T10:23:47.187Z
Answers: That seems to be identical to what has been suggested [2 years earlier](https://devops.stackexchange.com/a/4367/34805). Can you elaborate the difference?
Answers: stackprotector
Answers: CC BY-SA 4.0
Answers: 2020-04-17T11:14:00.737Z
Answers: 2020-04-17T12:22:17.803Z
Answers: 2020-04-17T12:22:17.803Z
Answers: Max Haase
Body: <p>Command run: <code>sudo ansible-playbook site.yml --connection=local</code></p>
  
  <pre>
  Distributor ID: Ubuntu
  Description:    Ubuntu 18.04 LTS
  Release:        18.04
  Codename:       bionic
  
  ansible 2.5.5
  </pre>
  
  <p>I'm encountering this error only on my first run through after I change the following variable in the following file:</p>
  
  <p>./group_vars/all</p>
  
  <pre>
  ntpserver: pool.ntp.org
  </pre>
  
  <p><br></p>
  
  <p><code>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
  </code></p>
  
  <p>On my second run through, I'm having no issues:</p>
  
  <p><code>TASK [common : Configure NTP file] **************************************************************************************************************
  ok: [127.0.0.1] =&gt; {"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}</code></p>
  
  <p>The folder and file structure I'm using is as follows:</p>
  
  <p><a href="https://github.com/ansible/ansible-examples/tree/master/lamp_simple_rhel7" rel="nofollow noreferrer">https://github.com/ansible/ansible-examples/tree/master/lamp_simple_rhel7</a></p>
  
  <p>./roles/common/handlers</p>
  
  <pre>
  - name: Restart NTP
    service: name=ntpd state=restarted`
  </pre>
  
  <p>./roles/common/tasks</p>
  
  <pre>
  ---
  - 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
  </pre>
  
Tags: <ansible>
Title: Ansible - Handler not found on first attempt
UserDisplayName: Eric Saindon
ThreadText: # Ansible - Handler not found on first attempt
  
  (Asked by: username_0 on 2018-06-23)
  
  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
  
  ```
  
  **Tags:** ansible
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2018-06-23. Score: 5
  
  The name of the handler "Restart NTP" does not match 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.
  
  
  ---
  
  ### Answer by username_2 on 2020-07-17. Score: 1
  
  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
  
  
  ---
  
  ### Answer by username_3 on 2020-04-17. Score: 0
  
  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
  
  ```
  
  > **Comments:**
  > **username_4**: That seems to be identical to what has been suggested [2 years earlier](https://devops.stackexchange.com/a/4367/34805). Can you elaborate the difference?  
  
  ---
  
  