# force file creation with template module

**URL:** <https://forum.ansible.com/t/force-file-creation-with-template-module/37330>\
**Category:** Ansible Project\
**Created:** [November 10, 2022, 1:41pm UTC](https://forum.ansible.com/t/force-file-creation-with-template-module/37330 "2022-11-10T13:41:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [November 10, 2022, 1:41pm UTC](https://forum.ansible.com/t/force-file-creation-with-template-module/37330/1 "2022-11-10T13:41:37Z")

</div>

I need to repair some templated files from a previous playbook execution

##### this is a test #########################################

&nbsp;&nbsp;- name: my template TEST - template postgres.conf  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- pg\_service\_name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: 00-ina-default.conf.j2 # 10-pg-service.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/home/postgres/{{ pg\_service\_name }}/00-ina-default.conf"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: postgres  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: postgres  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0600  
&nbsp;&nbsp;&nbsp;&nbsp;become: true  
&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- somedir

##### this was a test ########################################

this works whether dest does not exist. I want to overwrite an existing file though. This file possibly is or is not identical.

Can it be the force only works when the new and the old file differ?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 10, 2022, 1:49pm UTC](https://forum.ansible.com/t/force-file-creation-with-template-module/37330/2 "2022-11-10T13:49:48Z")

</div>

Ansible describes a state. In your case you want a file to look a certain way. The task will compare the output of the derived template with the existing file. If the existing file matches, no change is made. If the existing file differs or does not exist, it is overwritten or created. You don't need the "force" param.

Walter

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [November 10, 2022, 2:10pm UTC](https://forum.ansible.com/t/force-file-creation-with-template-module/37330/3 "2022-11-10T14:10:44Z")

</div>

that makes sense, tx.

In which kind of situatoin is force: true then even relevant?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 10, 2022, 2:20pm UTC](https://forum.ansible.com/t/force-file-creation-with-template-module/37330/4 "2022-11-10T14:20:50Z")

</div>

I would say force: true might be used when you simply want to force every system to get an update regardless of whether the existing file matches the template. I cannot think of a case where I personally would need that, but someone clearly thought this might be needed so the param is there.

Walter
