# Run custom code before first task in playbook

**URL:** <https://forum.ansible.com/t/run-custom-code-before-first-task-in-playbook/30116>\
**Category:** Ansible Project\
**Created:** [April 16, 2019, 5:25am UTC](https://forum.ansible.com/t/run-custom-code-before-first-task-in-playbook/30116 "2019-04-16T05:25:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavel\_Martynov](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@Pavel\_Martynov](https://forum.ansible.com/u/Pavel_Martynov)\
**Post date:** [April 16, 2019, 5:25am UTC](https://forum.ansible.com/t/run-custom-code-before-first-task-in-playbook/30116/1 "2019-04-16T05:25:21Z")

</div>

Hi!

I use json facts caching and sometimes this files expires (last modified time \> than configured fact\_caching\_timeout).  
This is a rare problem, but if it arises - I have non-obvious errors in stdout after running my playbook (like “ansible\_fqdn is not defined in host\_vars”, etc).

My idea: run some custom assertion code, that should check all cache files for expiration and stop further playbook run if there are some expired cache files with a good understandable message.  
How can I embed my custom check code in playbook? Maybe I should write some sort of ansible plugin?

Thanks.

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [April 16, 2019, 4:52pm UTC](https://forum.ansible.com/t/run-custom-code-before-first-task-in-playbook/30116/2 "2019-04-16T16:52:17Z")

</div>

I do something similar, I just use the assert module to check a fact that I know should be set to a predefined value.

`

- name: “linux : global assertions”  
assert:  
that:
- “group\_ld\_all\_000\_global\_defaults == ‘loaded’”  
fail\_msg: ‘environment issue, please double check, this is most likely a issue with fact cache expiring’  
`
