Question about force handlers

Testing out the force handlers parameter in a role and it is not working for me.The first tasks fails, so I though the force handlers would force the second one to get trigger anyway

nginx.yml


  • hosts: appservers

force_handlers: True

roles:

  • { role: nginx, when: “ansible_os_family == ‘RedHat’”}

tasks/main.yml

  • name: Run a command that fails

shell: /bin/false

  • name: Install nginx

yum:

name: nginx

state: installed

register: nginx_installed

notify:

  • restart nginx

handlers/main.yml

  • name: restart nginx
    service:
    name: nginx
    state: restarted
    register: nginx_restarted
  • debug: var=nginx_restarted

Output -

Testing out the force handlers parameter in a role and it is not working
for me.The first tasks fails, so I though the force handlers would force
the second one to get trigger anyway

As I understand the documentation it will only run the handlers that has been notified before the failed tasj. So any notify after the failed task will not trigger a handler.

nginx.yml

---
- hosts: appservers
   force_handlers: True
   roles:
     - { role: nginx, when: "ansible_os_family == 'RedHat'"}

tasks/main.yml

- name: Run a command that fails
   shell: /bin/false

- name: Install nginx
   yum:
     name: nginx
     state: installed
   register: nginx_installed
   notify:
      - restart nginx

Since shell task failes the yum is not run and the notify will not trigger the handler.

So I switched around the tasks in the task/main.yml to get this, and the notify still doesnt happen

tasks/main.yml

  • name: Install nginx
    yum:
    name: nginx
    state: installed
    register: nginx_installed
    notify:

  • restart nginx

  • name: Run a command that fails
    shell: /bin/false

Output -

Ok nevermind. This works now. I forgot to add become: yes to the main file in the last try. The task now calls the handler, despite the failure in the 2nd task.

So with the tasks/main.yml

tasks/main.yml

  • name: Install nginx

yum:

name: nginx

state: installed

register: nginx_installed

notify:

  • restart nginx

  • name: Run a command that fails

shell: /bin/false

Output -