how to flush handlers in a role.

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] **********************************************************************************************************************************************************************************************************************************

TASK [test_role : key from MX] **************************************************************************************************************************************************************************************************************
changed: [testmx1]

TASK [test_role : key from gateways] ********************************************************************************************************************************************************************************************************
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