# How instant is ansible.builtin.template?

**URL:** <https://forum.ansible.com/t/how-instant-is-ansible-builtin-template/8140>\
**Category:** Get Help\
**Tags:** template\
**Created:** [August 28, 2024, 4:17pm UTC](https://forum.ansible.com/t/how-instant-is-ansible-builtin-template/8140 "2024-08-28T16:17:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jure\_Sah](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jure_sah/32/4591_2.png) [@Jure\_Sah](https://forum.ansible.com/u/Jure_Sah)\
**Post date:** [August 28, 2024, 4:17pm UTC](https://forum.ansible.com/t/how-instant-is-ansible-builtin-template/8140/1 "2024-08-28T16:17:15Z")

</div>

I have a system under high usage load which is sensitive to partially written files. Basically you can imagine the file as a blacklist, because of the high load some requests might bypass the blacklist if there is ever an instant where ansible.builtin.template has partially written the file.

Obviously the workaround for this is to generate the file under a different filename and then rename it into place. However I don’t know how ansible.builtin.template functions internally. Does it already do something like this? Does the workaround make sense?

Basically what I’m asking is if ansible.builtin.template overwrites the file instantly, or will it truncate the file and then start writing the new file according to the loops in the template file?

LP,  
Jure

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 28, 2024, 5:24pm UTC](https://forum.ansible.com/t/how-instant-is-ansible-builtin-template/8140/2 "2024-08-28T17:24:59Z")

</div>

The template action templates the file on the controller, and then uses the copy action to copy the templated file to the controller. The copy action essentially copies the file over to a temp file, and then essentially calls the copy module to atomically move the file to its final destination. So the replacement should be instant.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 28, 2024, 5:58pm UTC](https://forum.ansible.com/t/how-instant-is-ansible-builtin-template/8140/3 "2024-08-28T17:58:51Z")

</div>

File alteration actions in Ansible should be using the ‘atomic\_move’ function.

> <https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L1588C1-L1589C32>

This function works as felix already explained, there is a case in which it writes directly to the file as a fallback, but only if you pass the `unsafe_writes=true` option.
