A playbook that creates one empty file ends its run with changed=1. Run it again and you get changed=1 again. Your host, inventory, and YAML are fine. The task is doing what its defaults say, and those defaults count as a change every time.
This guide covers why file with state: touch never settles to zero, the two-line fix, and how to check that the second run is clean.
What was and was not run
The bare playbook and its first-run recap come from a KodeKloud Engineer exercise: create an empty /tmp/file.txt on App Server 01. The exercise is written up in ndcodes' Day 85 post. That post shows a first-run recap with changed=1. It does not show a second run, and it does not state an Ansible version.
Everything labelled "expected, not executed" below is derived from the documented behaviour, not from a captured run. Run ansible --version on your control node and note it next to your results. The option names used here (modification_time, access_time, preserve) are the ones the Day 85 post quotes from the module documentation, so check them against the docs for your installed version.
Why touch reports changed every time
Most modules compare the system to a desired state and act only when there is a difference. state: touch can't work that way, because touching a file means updating its timestamps.
The module documentation, as quoted in the Day 85 post, says access_time and modification_time default to None. That means preserve is the default for state=file, directory, link and hard, while now is the default for state=touch.
So every run sets both timestamps to the current time. The filesystem really is different afterward, and Ansible reports that honestly. The file's existence is idempotent. Its mtime is not.
This is expected behaviour, not a bug. It still costs you, because a recap that always says changed teaches you to stop reading the changed count. In a playbook with forty tasks, one noisy task hides the change you actually care about.
Step 1: Check the file is absent first
Look before you run anything. If the file already exists, the task result looks identical to creating it, so this check is the only thing that separates "created" from "touched".
ls -l /tmp/file.txt
On a clean host you want No such file or directory. The exercise transcript ran ls /tmp and showed no file.txt in the listing.
Step 2: Run the bare task
This is the playbook from the exercise. It meets the requirement and has the always-changed problem.
---
- hosts: app_server
become: yes
tasks:
- name: Create empty file
file:
path: /tmp/file.txt
state: touch
The exercise reported this recap on the first run:
TASK [Create empty file] *************** changed: [stapp01]
PLAY RECAP *************** stapp01 : ok=2 changed=1 unreachable=0 failed=0
The ok=2 probably includes the implicit fact-gathering task, which would explain why it isn't 1. That is an inference; the post doesn't say. We cover related ground in google places api 120 results limit workaround (python) explained.
Step 3: Verify the file exists and is empty
After the run, confirm both facts. Test the "empty" claim instead of assuming it.
ls -l /tmp/file.txt
cat /tmp/file.txt
wc -c /tmp/file.txt
Expect the file listed, cat printing nothing, and wc -c returning 0. touch creates an empty file. A copy or lineinfile task would put content in it.
Step 4: Show the failure case
Run the same bare playbook a second time.
ansible-playbook -i inventory file.yml
Expected, not executed:
TASK [Create empty file] *************** changed: [stapp01]
PLAY RECAP *************** stapp01 : ok=2 changed=1 unreachable=0 failed=0
If you see changed=1 again with the file already present, you've reproduced the problem. Run stat /tmp/file.txt before and after. The Modify time should move forward on each run, and that is the change Ansible is reporting. See sharp vs cloudinary vs imgix for fixed avatar sizes: the details for additional background.
Step 5: Apply the fix
The module documentation gives this as an example, labelled "Touch again the same file, but do not change times this makes the task idempotent". Tell the module to leave both timestamps alone:
---
- hosts: app_server
become: yes
tasks:
- name: Create empty file, idempotently
ansible.builtin.file:
path: /tmp/file.txt
state: touch
modification_time: preserve
access_time: preserve
You keep the create-if-missing behaviour of touch. The fix only stops the module from rewriting timestamps on a file that is already there. That a brand-new file still gets created with preserve set is an inference from the documentation's example, so Step 6 checks it rather than trusting it.
Step 6: Verify both runs
Delete the file, then run the fixed playbook twice, in this order:
- Remove the file with
rm -f /tmp/file.txtand confirm it's gone withls. - Run the fixed playbook. Expected, not executed:
changed=1, because the file did not exist. - Run
ls -l /tmp/file.txtandcat /tmp/file.txt. You want the file present and no output fromcat. - Note the timestamp with
stat /tmp/file.txt. - Run the playbook again. Expected, not executed:
ok=2 changed=0. - Run
statonce more. The Modify time should match step 4.
If step 5 still says changed=1, check that you saved the fixed file and that you're running the right playbook. Also make sure a second task or a handler isn't the one reporting the change.
The become caveat: owner and mode
With become: yes, root creates the file, so root owns it. The documentation says that when owner is unspecified, the module uses the current user unless you are root, in which case it can preserve the previous ownership. A file that didn't exist has no previous ownership, so root it is.
Also read: rds inaccessible encryption credentials: kms triage guide
That's fine for a scratch file in /tmp. For anything real, set ownership explicitly:
- name: Create empty file, idempotently
ansible.builtin.file:
path: /tmp/file.txt
state: touch
modification_time: preserve
access_time: preserve
owner: tony
group: tony
mode: "0644"
The user and group here are placeholders. Use whoever should own the file. Setting mode also means the permissions no longer depend on the remote umask, which is an inference from general Linux behaviour, not from the Day 85 post. The same goes for the second run: once the attributes match, they should stop producing changes, so changed=0 is expected, not confirmed.
Touch, copy, or something else?
If all you need is for the file to exist, this table is a starting point. The copy and state=file rows are inference from how those modules generally behave, not from the exercise.
| Module | Creates a missing file | Safe to rerun | Watch out for |
|---|---|---|---|
| file with state: touch plus preserve times | Yes | Yes, changed=0 once present | Set owner and mode under become |
| file with bare state: touch | Yes | No, timestamps update every run | Permanent changed=1 |
| file with state: file | No, it will not create the file | Yes | Meant for fixing attributes on an existing file |
| copy with content: "" | Yes | Yes, when content matches | Can overwrite existing content with nothing |
If you want the file to exist and don't care about its contents, use touch with preserve. If bumping the timestamp is the actual goal, bare touch is the right tool and the changed result is accurate.
What to do next
Once this task settles to changed=0, search your other playbooks for bare state: touch. Each one adds a permanent change to its recap. Fix them and the changed count becomes a signal again, so a nonzero number on a second run means something actually moved.



