# running handlers

**URL:** <https://forum.ansible.com/t/running-handlers/38726>\
**Category:** Ansible Project\
**Created:** [March 22, 2024, 6:15pm UTC](https://forum.ansible.com/t/running-handlers/38726 "2024-03-22T18:15:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [March 22, 2024, 6:15pm UTC](https://forum.ansible.com/t/running-handlers/38726/1 "2024-03-22T18:15:03Z")

</div>

i’m trying to copy a bunch of ssl ca certs into a directory. this works file with the copy module and if it sees a new one it runs a handler to rehash all the certs in the directory to create \*.0 symlinks to each of the certs

however, if an additional cert gets put in there by someone and they don’t rehash the directory, now there’s a cert without a hash

i can count the \*.crt’s and the \*.0’s using the find module, which puts a ‘matched’ number into the ansible registered variable from the find, but i can’t figure a way to run the handler to rehash the directory if the counts aren’t equal

i found some other examples where people just run handlers based on command: /bin/true with changed\_when: true, but i don’t want to run it all the time, only if the counts aren’t equal

any suggestions?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 22, 2024, 8:01pm UTC](https://forum.ansible.com/t/running-handlers/38726/2 "2024-03-22T20:01:12Z")

</div>

FWIW, instead of handlers, get the lists of the certs and  
links, and iterate the difference

&nbsp;&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /tmp/ansible/certs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: '\*.crt'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_crt

&nbsp;&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /tmp/ansible/certs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: '\*.0'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: link  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_lnk

&nbsp;&nbsp;&nbsp;&nbsp;- file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: link  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /tmp/ansible/certs/{{ item }}.crt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/ansible/certs/{{ item }}.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ certs | difference(links) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;certs: "{{ out\_crt.files |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.path') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('basename') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('splitext') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('first') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;links: "{{ out\_lnk.files |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.path') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('basename') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('splitext') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('first') }}"

Fit the paths to your needs.

---

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [March 23, 2024, 3:12pm UTC](https://forum.ansible.com/t/running-handlers/38726/3 "2024-03-23T15:12:20Z")

</div>

thanks, eventually after a few hours i came to a similar conclusion, i was hoping there was a more elegant way which didn’t need a series of finds

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 23, 2024, 8:50pm UTC](https://forum.ansible.com/t/running-handlers/38726/4 "2024-03-23T20:50:54Z")

</div>

The complexity is a consequence of you requirement to  
"rehash the directory if the counts aren't equal". You  
can't compare \*counts\* without at least two \*finds\*. The  
elegant way is a single \*find\* and iteration of all  
certificates

&nbsp;&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /tmp/ansible/certs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: '\*.crt'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_crt

&nbsp;&nbsp;&nbsp;&nbsp;- file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: link  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /tmp/ansible/certs/{{ item }}.crt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/ansible/certs/{{ item }}.0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ out\_crt.files |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.path') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('basename') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('splitext') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('first') }}"

This is, however, less efficient. The options are limited  
because \*file\* doesn't work with wildcards. If you don't  
care about idempotency and the changed/failed status  
\*shell\* also does the job

&nbsp;&nbsp;&nbsp;&nbsp;- shell: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cd /tmp/ansible/certs &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i in \*.crt; do ln -s $i ${i%.crt}.0; done  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed\_when: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: false

You can write advanced scripts or custom filters that would  
do what you want.
