Test the flag restart_httpd in the handler
handlers:
- name: Restart httpd
debug:
msg: Restart httpd
when: restart_httpd|bool
Given the certificates below, let's assume none of them has been installed yet
shell> tree files
files
├── cert1.pem
├── cert2.pem
└── cert3.pem
The task will copy the certificates and register the changes
- copy:
src: "files/{{ item }}"
dest: "{{ cert_path }}/{{ item }}"
loop: "{{ list_cert_names }}"
register: cert_copied
notify: Restart httpd
- debug:
var: cert_copied.results|map(attribute='changed')|list
- debug:
var: cert_copied.changed
gives
cert_copied.results|map(attribute='changed')|list:
- true
- true
- true
cert_copied.changed: true
Because the task was changed the handler will be notified. If the flag is true the handler will execute the task
RUNNING HANDLER [Restart httpd] ****************************************
ok: [localhost] =>
msg: Restart httpd
There is no need to explicitly test the changes. For example, the task won't be changed if no file is copied
TASK [copy] ************************************************************
ok: [localhost] => (item=cert1.pem)
ok: [localhost] => (item=cert2.pem)
ok: [localhost] => (item=cert3.pem)
TASK [debug] ***********************************************************
ok: [localhost] =>
cert_copied.results|map(attribute='changed')|list:
- false
- false
- false
TASK [debug] ***********************************************************
ok: [localhost] =>
cert_copied.changed: false
You can see that there were no changes and, as a result, the handler wasn't notified. If, for example, only one file is changed the handler will be notified
TASK [copy] ************************************************************
ok: [localhost] => (item=cert1.pem)
changed: [localhost] => (item=cert2.pem)
ok: [localhost] => (item=cert3.pem)
TASK [debug] ***********************************************************
ok: [localhost] =>
cert_copied.results|map(attribute='changed')|list:
- false
- true
- false
TASK [debug] ***********************************************************
ok: [localhost] =>
cert_copied.changed: true
RUNNING HANDLER [Restart httpd] ****************************************
ok: [localhost] =>
msg: Restart httpd