Search code examples
linuxansibledevopsansible-2.xansible-handlers

ansible debug msg throws error inside handler


Playbook runs fine but debug msg module throws undefined variable error.

---
- hosts: labservers
 become: yes
 tasks:
- name: restart httpd
 service:
     name: httpd
     state: restarted
 notify:
     - cmds
 handlers:
- name: change file details
replace:
    path: /etc/httpd/conf/magic
    regexp: 'its is nothing'
    replace: '100 notes ashes'
register: httpd_data
- name: commands out
debug:
    msg: "{{httpd_data}}"
listen: "cmds"

code in image for clear understanding


Solution

  • Solved by calling both the tasks in handlers using listen.

    ---
    - hosts: labservers
    become: yes
    tasks:
    - name: restart httpd
    service:
         name: httpd
         state: restarted
    notify:
         - cmds
    handlers:
    - name: change file details
    replace:
        path: /etc/httpd/conf/magic
        regexp: '100 notes ashes'
        replace: '100 akash'
    register: httpd_data
    listen: "cmds"
    - name: debug details
    debug:
         msg:"{{ httpd_data }}"
    listen: "cmds"