Role Handler Separation

76 views
Skip to first unread message

John Petro

unread,
Feb 8, 2024, 6:42:06 PM2/8/24
to ansible...@googlegroups.com
Good Evening...  A coworker of mine is working on finishing up a role, that may be use across multiple operating systems.  We were wondering, if there was a way to separate the handler files into files based on OS, that are only parsed when you are running the role against that particular OS. 

For example, if you were running the role against a windows server, the handler would be in windows.yml, or if it was debian, then the handler would be in Debian.yml.

I know how to do this for the tasks files, but wasn't sure if this was possible for handlers files.

--John

Daniel Barros

unread,
Feb 9, 2024, 11:59:09 AM2/9/24
to Ansible Project
Would something like that help?

---
- name: Playbook Temp
  hosts: localhost
  gather_facts: true
  vars:
    restart_svcs:
      Debian: Restart Debian service
      RedHat: Restart RedHat service

  handlers:
    - name: Restart Debian service
      ansible.builtin.debug:
        msg: "some debian command"

    - name: Restart RedHat service
      ansible.builtin.debug:
        msg: "some red hat command"

  tasks:
    - name: Change File
      ansible.builtin.debug:
        msg: "Simulate file change"
      changed_when: true
      notify: "{{ restart_svcs[ansible_os_family] }}"

Vinay Mulugund

unread,
Feb 12, 2024, 9:32:04 AM2/12/24
to Ansible Project

John Petro

unread,
Feb 14, 2024, 3:24:33 PM2/14/24
to ansible...@googlegroups.com
I did see that...  I have to go back and try it again, but I think it was still reading in all of the handlers for some reason.  I did think about just putting the handler in that tasks file too...  hadn't yet tried that one, but I am pretty sure that would work too.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/88253787-22e0-43cd-ad6a-a3362a1f717fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages