AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible file Module: Create Files, Directories & Manage Permissions

By Luca Berton · Published 2024-01-01 · Category: installation

How to create empty files with Ansible file module. Use state=touch, set permissions, create flag files, and manage file existence with examples.

Create an empty file - Ansible module file How to create an empty ~/example.txt file or update the time of an already create file.

How to create an empty file with Ansible? I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot

Ansible create an empty file Today we're talking about the Ansible module file. The full name is ansible.builtin.file, which means that is part of the collection of modules "builtin" with ansible and shipped with it. It's a module pretty stable and out for years. It works in a different variety of operating systems. It manages files and file properties. For Windows targets, use the ansible.windows.win_file module instead.

Main Parameters • path string (dest, name) - file path • state string - file/absent/directory/hard/link/touch

This module has some parameters to perform any tasks. The only required is "path", where you specify the filesystem path of the file you're going to edit. The state defines the type of object we are modifying, the default is "file" but for our use case, we need the "touch" option.

## Playbook Let's jump into a real-life playbook on how to create an empty file with Ansible. • file.yml

code with ❤️ in GitHub

Conclusion

Now you know how to create an empty file or update access time with Ansible.

Advanced File Creation Examples

Create file with specific ownership and permissions

Create file only if it doesn't exist

The touch state always updates the timestamp. To create only if missing:

Create multiple files

Create a file with content (use copy module instead)

If you need content in the file, use ansible.builtin.copy:

Create parent directories automatically

The file module with state: touch requires parent directories to exist. Create them first:

touch vs copy vs template

| Module | Use Case | |--------|----------| | file (state=touch) | Create empty file or update timestamp | | copy (content=) | Create file with static content | | template | Create file from Jinja2 template | | lineinfile | Add/modify a single line in existing file | | blockinfile | Add/modify a block of text in existing file |

FAQ

What's the difference between state: touch and state: file? • touch: Creates the file if it doesn't exist, updates timestamp if it does • file: Only modifies attributes (owner, mode) of an existing file; fails if file doesn't exist

How do I create a file on Windows?

Use ansible.windows.win_file:

Create Empty File

Create with Parent Directories

Create Multiple Empty Files

Create Only If Not Exists

Create File with Content

touch vs copy (empty)

| Method | Creates | Updates Timestamp | Overwrites | |--------|---------|------------------|------------| | file: state=touch | Yes | Always | No (preserves content) | | copy: content="" | Yes | Only if changed | Yes (empties file) | | copy: force=false | Yes | No | No |

Set Access/Modification Time

FAQ

Does touch overwrite existing file content?

No — state: touch creates an empty file OR updates the timestamp of an existing file without changing its content. It's safe to use on files with data.

Why does touch always report "changed"?

Because it updates the timestamp every run. To avoid this, use copy: content="" force=false or add a stat check first.

How do I create an empty file on Windows?

Create Empty File

With Permissions

Create Only If Missing

Create with copy (Content)

Marker/Flag Files

Multiple Empty Files

touch vs file state=file

FAQ

Does touch update the modification time?

Yes — state: touch always updates mtime, even if the file exists. Use a stat check first to avoid this.

How to create an empty file with specific content later?

Use copy: content="" dest=/path force=false to create empty, then later copy: content="data" or template to fill it.

touch vs copy with empty content?

touch updates mtime every run (always "changed"). copy: content="" force=false only creates if missing (idempotent).

Create Empty File

With Permissions

Create Only If Missing

Multiple Empty Files

Flag File Pattern

touch vs copy for Empty Files

Modify Timestamp

FAQ

touch changes timestamp — is that a problem?

state: touch always reports changed because it updates the timestamp. If you only want to create (not update), check with stat first.

How to create a file with content?

Use copy module with content parameter instead of file + touch.

Can I create files in directories that don't exist?

No — parent directories must exist. Create them first with file: { path: ..., state: directory }.

Related Articlesansible.builtin.file GuideAnsible for Windows Guide

Category: installation

Watch the video: Ansible file Module: Create Files, Directories & Manage Permissions — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home