<blockquote>
<p><em>The most obvious candidate to this task is <code>ansible.builtin.file</code> with <code>state: touch</code>.</em></p>
</blockquote>
<p>Right, this could be an option.</p>
<blockquote>
<p><em>However, it always appears as changed as the times need to be updated.</em></p>
</blockquote>
<p>According the documentation <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#parameters" rel="nofollow noreferrer"><code>file</code> module - Parameters</a> not necessarily since <code>access_time</code> and <code>modification_time</code></p>
<blockquote>
<p><em>Should be <code>preserve</code> when no modification is required</em></p>
</blockquote>
<p>The following minimal example</p>
<pre class="lang-yaml prettyprint-override"><code>---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Create file
    file:
      path: &quot;/home/{{ ansible_user }}/test.file&quot;
      owner: &quot;{{ ansible_user }}&quot;
      group: &quot;users&quot;
      access_time: preserve
      modification_time: preserve
      state: touch

  - name: Touch again
    file:
      path: &quot;/home/{{ ansible_user }}/test.file&quot;
      owner: &quot;{{ ansible_user }}&quot;
      group: &quot;users&quot;
      access_time: preserve
      modification_time: preserve
      state: touch
</code></pre>
<p>will result into an output of</p>
<pre class="lang-yaml prettyprint-override"><code>TASK [Create file] ***************************
changed: [localhost]

TASK [Touch again] ***************************
ok: [localhost]

PLAY RECAP ***********************************
localhost                  : ok=2    changed=1 
</code></pre>

CC BY-SA 4.0
2022-12-02T17:00:00.573Z
2022-12-02T17:00:00.573Z
U880D
<p>The task seems simple. If the file does not exist, create it, otherwise retain the file and its content. In both cases, apply permissions (<code>owner</code>, <code>group</code>, <code>mode</code>).</p>
<p>The most obvious candidate to this task is <code>ansible.builtin.file</code> with <code>state: touch</code>. However, it always appears as changed as the times need to be updated. Rather, once it exists and modes are ok, it should just be ok.</p>
<p>The next candidate likely is <code>ansible.builtin.copy</code>. If passing <code>force: false</code>, it creates the file. However, it does not fix up permission on an already existing file.</p>

<ansible>
How to create, but not overwrite, a file and manage its permissions with ansible?
Helmut Grohne
# How to create, but not overwrite, a file and manage its permissions with ansible?

(Asked by: username_0 on 2022-12-02)

The task seems simple. If the file does not exist, create it, otherwise retain the file and its content. In both cases, apply permissions (`owner`, `group`, `mode`).

The most obvious candidate to this task is `ansible.builtin.file` with `state: touch`. However, it always appears as changed as the times need to be updated. Rather, once it exists and modes are ok, it should just be ok.

The next candidate likely is `ansible.builtin.copy`. If passing `force: false`, it creates the file. However, it does not fix up permission on an already existing file.

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2022-12-02. Score: 3 | Accepted answer

The most obvious candidate to this task is ansible.builtin.file with state: touch.

Right, this could be an option.

However, it always appears as changed as the times need to be updated.

According the documentation file module - Parameters not necessarily since access_time and modification_time

Should be preserve when no modification is required

The following minimal example
---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Create file
    file:
      path: "/home/{{ ansible_user }}/test.file"
      owner: "{{ ansible_user }}"
      group: "users"
      access_time: preserve
      modification_time: preserve
      state: touch

  - name: Touch again
    file:
      path: "/home/{{ ansible_user }}/test.file"
      owner: "{{ ansible_user }}"
      group: "users"
      access_time: preserve
      modification_time: preserve
      state: touch

will result into an output of
TASK [Create file] ***************************
changed: [localhost]

TASK [Touch again] ***************************
ok: [localhost]

PLAY RECAP ***********************************
localhost                  : ok=2    changed=1


---

