57

I'm using Ansible 1.6.6 to provision my machine.

There is a template task in my playbook that creates destination file from Jinja2 template:

tasks:
    - template: src=somefile.j2 dest=/etc/somefile.conf

I do not want to replace somefile.conf if it already exists. Is it possible with Ansible? If so, how?

Slava Fomin II
  • 1,701
  • 4
  • 17
  • 23

4 Answers4

69

You can just use the force param of the template module with force=no

tasks:
  - name: Create file from template if it doesn't exist already.
    template: 
      src: somefile.j2
      dest: /etc/somefile.conf
      force: no

From the Ansible template module docs:

force: the default is yes, which will replace the remote file when contents are different than the source. If no, the file will only be transferred if the destination does not exist.

Other answers use stat because the force parameter was added after they were written.

4wk_
  • 310
  • 3
  • 15
sanzante
  • 709
  • 5
  • 12
  • 6
    my answer uses stat, because at the time of the question/answer there was no force argument avaialable for template – Teftin Jul 09 '16 at 13:13
  • i was hoping to use this for a full path of files (not templates), hence it looks like template doesnt work – Brian Thomas Sep 21 '22 at 16:05
  • 1
    @BrianThomas, I'm not sure what you mean but if you want to apply this to a file copy instead of a template you can use copy module, is supports the force parameter. See https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html – sanzante Sep 23 '22 at 08:39
68

You can check for file existence using stat, and then use template only if file does not exist.

tasks:
  - stat: path=/etc/somefile.conf
    register: st
  - template: src=somefile.j2 dest=/etc/somefile.conf
    when: not st.stat.exists
Teftin
  • 1,929
  • 17
  • 9
  • There is now an option `force` to the [Ansible template module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html#parameter-force) that does precisely that. This option was introduced a long time ago (years), but I don't find when in the patch notes. – 4wk_ Apr 17 '23 at 08:26
11

You can first check that the destination file exists or not and then make a decision based on the output of it's result.

tasks:
  - name: Check that the somefile.conf exists
    stat:
      path: /etc/somefile.conf
    register: stat_result

  - name: Copy the template, if it doesnt exist already
    template:
      src: somefile.j2
      dest: /etc/somefile.conf
    when: stat_result.stat.exists == False   
arbabnazar
  • 529
  • 6
  • 9
-1

According to me, the easiest solution is to use the attribute "force=no" from the template module

  • 2
    Back when this question was asked, the ```force``` parameter was not implemented. Today, this answer describes best practice! – dirdi Aug 05 '20 at 13:15