# Hooks in Ansible

**URL:** <https://forum.ansible.com/t/hooks-in-ansible/36900>\
**Category:** Ansible Project\
**Created:** [July 25, 2022, 5:06pm UTC](https://forum.ansible.com/t/hooks-in-ansible/36900 "2022-07-25T17:06:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangoncalves](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dangoncalves/32/2427_2.png) [@dangoncalves](https://forum.ansible.com/u/dangoncalves)\
**Post date:** [July 25, 2022, 5:06pm UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/1 "2022-07-25T17:06:33Z")

</div>

Hello,

I wonder if you already had some discussions about hooks in Ansible. I tried some research but found nothing useful about what I am looking for.

What I want to do is executing some code at the end of the playbook execution, in other words executing some code at some arbitrary playbook execution step. I wonder if there is some event listener I can subscribe on in order to execute my code.

I don't know if this is good place to discuss about that or if I need to start a thread on devel. If so let me know.

Daniel.

---

<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:** [July 25, 2022, 7:55pm UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/2 "2022-07-25T19:55:24Z")

</div>

This depends on the use case:

1) To execute the code at the end of the playbook you can use  
&nbsp;&nbsp;&nbsp;\*post\_tasks\*. At some arbitrary playbook execution step use  
&nbsp;&nbsp;&nbsp;\*tasks\*.

2) To trigger the execution of a code by some events you can use  
&nbsp;&nbsp;&nbsp;the module \*wait\_for\*  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait\_for\_module.html#ansible-builtin-wait-for-module-waits-for-a-condition-before-continuing](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html#ansible-builtin-wait-for-module-waits-for-a-condition-before-continuing)  
&nbsp;&nbsp;&nbsp;and \*handlers\*  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_handlers.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html)

3) To execute the code after the playbook finished you can use  
&nbsp;&nbsp;&nbsp;\*ansible-runner\*  
&nbsp;&nbsp;&nbsp;[https://ansible-runner.readthedocs.io/en/stable/python\_interface/#usage-examples](https://ansible-runner.readthedocs.io/en/stable/python_interface/#usage-examples)

---

<div class="post-metadata">

**Author:** ![dangoncalves](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dangoncalves/32/2427_2.png) [@dangoncalves](https://forum.ansible.com/u/dangoncalves)\
**Post date:** [July 26, 2022, 5:32am UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/3 "2022-07-26T05:32:33Z")

</div>

Thank you for you answer.

I already know \*post\_tasks\*, \*handlers\*, and \*ansible-runner\*, but I don't think there are good for what I'm looking for.

You need a bit more context: I have a custom lookup plugin that initialize some data and execute a new process in order to keep these datas available during the playbook life.  
I want to be able to send a SIGTERM signal to the process to terminate it at the end of playbook execution.

As I want to share this custom plugin, I don't want any user interaction, because it's not the user role to worry about this extra process.  
So \*post\_tasks\*, \*handlers\*, and \*wait\_for\* are excluded. \*ansible-runner\* may be a good alternative but I don't want to force users to use \*ansible-runner\* instead of \*ansible\*.

I hope it's more clear.

Regards,

Daniel

---

<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:** [July 26, 2022, 8:57am UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/4 "2022-07-26T08:57:16Z")

</div>

This seems to be a candidate for \*async\*  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_async.html#run-tasks-concurrently-poll-0](https://docs.ansible.com/ansible/latest/user_guide/playbooks_async.html#run-tasks-concurrently-poll-0)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 26, 2022, 11:08am UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/5 "2022-07-26T11:08:13Z")

</div>

Sounds like you want cache plugins.  
[https://docs.ansible.com/ansible/latest/plugins/cache.html](https://docs.ansible.com/ansible/latest/plugins/cache.html)

---

<div class="post-metadata">

**Author:** ![dangoncalves](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dangoncalves/32/2427_2.png) [@dangoncalves](https://forum.ansible.com/u/dangoncalves)\
**Post date:** [July 26, 2022, 3:46pm UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/6 "2022-07-26T15:46:01Z")

</div>

Currently my plugin's process has a Time To Live concept to keep data during some customizable duration.  
It's pretty similar to what \*async\* provides but the drawback is that you need to know in advance how many time the execution will take, and that's what I want to avoid.

Regards,

Daniel

---

<div class="post-metadata">

**Author:** ![dangoncalves](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dangoncalves/32/2427_2.png) [@dangoncalves](https://forum.ansible.com/u/dangoncalves)\
**Post date:** [July 26, 2022, 3:47pm UTC](https://forum.ansible.com/t/hooks-in-ansible/36900/7 "2022-07-26T15:47:03Z")

</div>

Hello, thank you for your answer!

What I understand about cache plugins is that what could interrest me is  
fact caching. But as far as I know, lookup plugins can't set facts.

Regards,

Daniel
