Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active November 5, 2021 18:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halberom/3553a00e24468b02d8f12ed73fa33f85 to your computer and use it in GitHub Desktop.
Save halberom/3553a00e24468b02d8f12ed73fa33f85 to your computer and use it in GitHub Desktop.
ansible - example of calling a handler when a fact is set
---
- hosts: localhost
connection: local
gather_facts: False
tasks:
- debug: msg="call handler"
changed_when: True
notify: do something
handlers:
- name: do something
debug: msg="I'm doing something"
when: foo|default(False)|bool
- hosts: localhost
connection: local
gather_facts: False
tasks:
- set_fact:
foo: True
- debug: msg="call handler"
changed_when: True
notify: do something
handlers:
- name: do something
debug: msg="I'm doing something"
when: foo|default(False)|bool
$ ansible-playbook ~/tmp.yml -i '127.0.0.1,'
PLAY [localhost] ***************************************************************
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"msg": "call handler"
}
RUNNING HANDLER [do something] *************************************************
skipping: [127.0.0.1]
PLAY [localhost] ***************************************************************
TASK [set_fact] ****************************************************************
ok: [127.0.0.1]
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"msg": "call handler"
}
RUNNING HANDLER [do something] *************************************************
ok: [127.0.0.1] => {
"msg": "I'm doing something"
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=4 changed=2 unreachable=0 failed=0
@ravi-pmp
Copy link

Nice and helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment