Question about force handlers

53 views
Skip to first unread message

Anfield

unread,
Sep 21, 2017, 10:11:03 AM9/21/17
to Ansible Project
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 -  

[ansible@localhost roles]$ ansible-playbook nginx.yml

PLAY [appservers] ***********************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [10.10.0.3]
ok: [10.10.0.4]

TASK [nginx : Run a command that fails] *************************************************
fatal: [10.10.0.3]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.017543", "end": "2017-09-21 10:09:03.816771", "failed": true, "rc": 1, "start": "2017-09-21 10:09:03.799228", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
fatal: [10.10.0.4]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta": "0:00:00.006919", "end": "2017-09-21 10:08:51.790017", "failed": true, "rc": 1, "start": "2017-09-21 10:08:51.783098", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
        to retry, use: --limit @/etc/ansible/playbooks/roles/nginx.retry

PLAY RECAP ******************************************************************************
10.10.0.3                  : ok=1    changed=0    unreachable=0    failed=1
10.10.0.4                  : ok=1    changed=0    unreachable=0    failed=1

 

Kai Stian Olstad

unread,
Sep 21, 2017, 1:49:28 PM9/21/17
to ansible...@googlegroups.com
On 21. sep. 2017 16:11, Anfield wrote:
> 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.


--
Kai Stian Olstad

Anfield

unread,
Sep 21, 2017, 3:05:31 PM9/21/17
to Ansible Project
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 - 

[ansible@localhost roles]$ ansible-playbook nginx.yml

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [nginx : Install nginx] ***************************************************
fatal: [localhost]: FAILED! => {"changed": true, "failed": true, "msg": "Repository epel is listed more than once in the configuration\nRepository epel-source is listed more than once in the configuration\nYou need to be root to perform this command.\n", "rc": 1, "results": ["Loaded plugins: fastestmirror, langpacks\n"]}
        to retry, use: --limit @/etc/ansible/playbooks/roles/nginx.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1



Anfield

unread,
Sep 21, 2017, 3:22:58 PM9/21/17
to Ansible Project
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 - 

[ansible@localhost roles]$ ansible-playbook nginx.yml

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [nginx : Install nginx] ***************************************************
changed: [localhost]

TASK [nginx : Run a command that fails] ***************************************             *
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "/bin/false", "delta":              "0:00:00.003499", "end": "2017-09-21 15:19:14.897224", "failed": true, "rc": 1,              "start": "2017-09-21 15:19:14.893725", "stderr": "", "stderr_lines": [], "stdo             ut": "", "stdout_lines": []}

RUNNING HANDLER [nginx : restart nginx] ***************************************             *
changed: [localhost]
        to retry, use: --limit @/etc/ansible/playbooks/roles/nginx.retry

PLAY RECAP ********************************************************************             *
localhost                  : ok=3    changed=2    unreachable=0    failed=1
 
Reply all
Reply to author
Forward
0 new messages