# how to flush handlers in a role.

**URL:** <https://forum.ansible.com/t/how-to-flush-handlers-in-a-role/34993>\
**Category:** Ansible Project\
**Created:** [April 15, 2021, 3:12pm UTC](https://forum.ansible.com/t/how-to-flush-handlers-in-a-role/34993 "2021-04-15T15:12:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Mario\_Garcia](https://forum.ansible.com/u/Mario_Garcia)\
**Post date:** [April 15, 2021, 3:12pm UTC](https://forum.ansible.com/t/how-to-flush-handlers-in-a-role/34993/1 "2021-04-15T15:12:12Z")

</div>

I am wondering how can i use :

- meta: flush\_handlers in a role. so that the handlers are executed immediately after a task in the role has changed.

i have these tasks in a role:

- name: key from MX  
shell: cat /server/.ssh/id\_dsa.pub  
become: true  
register: id\_dsa\_pub  
when: inventory\_hostname in groups[‘MX’]  
notify: add to authorized\_keys

- name: key from gateways  
shell: cat /root/.ssh/id\_rsa.pub  
become: true  
register: id\_dsa\_pub  
when: inventory\_hostname in groups[‘GW’]  
notify: add to authorized\_keys

my site.yml is like this:

- name: apply common configuration to all nodes  
hosts: all  
remote\_user: ansible  
become: true  
tags: custom

tasks:

- meta: flush\_handlers

roles:

- common

and i call the playbook like this:

ansbible-playbook -i inventory site.yml --limit=‘MX’

i prefer this method to have to add a play to every hostgroup and is more generic as our users just call the playbook and limit to the group they want.

the problem is that the keys from the MX are not added because of an undefined variable.

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [testmx1]

RUNNING HANDLER [test\_role : add to authorized\_keys] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [testmx1]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘stdout’\n\nThe error appears to be in ‘/home/ansible/roles/test\_role/handlers/main.yml’: line 4, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n#\n- name: add to authorized\_keys \n ^ here\n”}

I would like not to have to fetch the remote authorized keys to localhost first but using

if i put these in a simple playbook (not a role) and add the flush\_handler meta tag it works as expected but not when in a role.  
or  
how to dynamically adding the task based on the ansible\_limit magic variable?

thanks
