Skip to content
Boomspot
  • Home
Loading...
Boomspot

Daily tech news, software development coverage, Apple reporting, and the gear behind modern music making.

TwitterLinkedIn

Browse

  • Categories
  • Tags
  • Authors

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Service
  • Unsubscribe

© 2026 Boomspot. All rights reserved.

Built by Boomspot
Updated hourly

AI Content Disclosure: Articles on Boomspot are researched, written, and edited with the assistance of advanced AI systems. We combine software-assisted research with editorial oversight to deliver useful, accurate, and practical technical and music production content. Learn more about our editorial approach.

Browse by Category

Technology681Coding193SEO66Linux66Studio Gear47Music Production42Apple Rumors11

Popular Posts

Speed Up Linux Kernel Compile Time: 7 Practical Fixes

Speed Up Linux Kernel Compile Time: 7 Practical Fixes

8 min read
Is Linux on PS5 Worth It? pLinux5-NG vs. a Mini PC

Is Linux on PS5 Worth It? pLinux5-NG vs. a Mini PC

7 min read
How to Test Budget DJ Headphones and Speakers at Home

How to Test Budget DJ Headphones and Speakers at Home

10 min read
Beginner-Friendly Terminal Editor: TTT vs micro vs Edit

Beginner-Friendly Terminal Editor: TTT vs micro vs Edit

8 min read
Affiliate Site Audit: Measure Your Google Traffic Risk

Affiliate Site Audit: Measure Your Google Traffic Risk

11 min read

Recent Posts

Google Automated Calls Business Profile Setting Audit

Google Automated Calls Business Profile Setting Audit

Nov 20, 2026•9 min
Voice Agent Latency Budget: Where a Turn's 1,000 ms Goes

Voice Agent Latency Budget: Where a Turn's 1,000 ms Goes

Nov 20, 2026•8 min
Yamaha NS-10 Alternatives for a Home Studio: What to Buy

Yamaha NS-10 Alternatives for a Home Studio: What to Buy

Nov 20, 2026•7 min
Check Installed OpenJDK Versions on Linux After Java4Shell

Check Installed OpenJDK Versions on Linux After Java4Shell

Nov 20, 2026•9 min
How to Measure AI Agent Oversight Metrics in Your Repo

How to Measure AI Agent Oversight Metrics in Your Repo

Nov 20, 2026•8 min
  1. Home
  2. Coding
  3. Ansible Touch Always Changed: Make the File Task Idempotent
coding7 min read

Ansible Touch Always Changed: Make the File Task Idempotent

Your playbook creates an empty file, and the recap says changed=1 on every run. It is not a bug. Two timestamp defaults cause it, and two lines of YAML fix it.

S

Staff

November 20, 2026

Reviewed byDorian

Ansible Touch Always Changed: Make the File Task Idempotent

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:

  1. Remove the file with rm -f /tmp/file.txt and confirm it's gone with ls.
  2. Run the fixed playbook. Expected, not executed: changed=1, because the file did not exist.
  3. Run ls -l /tmp/file.txt and cat /tmp/file.txt. You want the file present and no output from cat.
  4. Note the timestamp with stat /tmp/file.txt.
  5. Run the playbook again. Expected, not executed: ok=2 changed=0.
  6. Run stat once 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.

Tags

Coding TutorialsCoding Best PracticesDeveloper ToolsOpen-sourceWeb DevelopmentDevOps

Keep reading

Unlocking ChatGPT Developer Mode: Full MCP Client Access
Coding•4 min read

Unlocking ChatGPT Developer Mode: Full MCP Client Access

Unlock the power of ChatGPT Developer Mode with full MCP client access. Discover how to enhance your coding projects and streamline development.

Sep 11, 2025

WebAssembly: Unleashing Native Speed in Web Browsers
Coding•4 min read

WebAssembly: Unleashing Native Speed in Web Browsers

WebAssembly is transforming web development with near-native performance, enabling more complex and efficient applications.

Sep 6, 2025

Your Guide to GitHub Universe 2025: Schedule Launched!
Coding•3 min read

Your Guide to GitHub Universe 2025: Schedule Launched!

Get ready for GitHub Universe 2025! Check out the schedule, create your personalized agenda, and sign up for mentoring sessions. Join us for an exciting experience!

Sep 13, 2025

More stories for your next project

Get tech, coding, and music production updates in your inbox.

Unsubscribe anytime.