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 — Video Tutorial
How to create empty files with Ansible file module. Use state=touch, set permissions, create flag files, and manage file existence with examples.
What You'll Learn
- Create an empty file - Ansible module file
- How to create an empty file with Ansible?
- Ansible create an empty file
- Main Parameters
- Conclusion
- Advanced File Creation Examples
- Create file with specific ownership and permissions
- Create file only if it doesn't exist
- Create multiple files
- Create a file with content (use copy module instead)
Full Tutorial Content
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
```yaml
---
- name: file module demo
hosts: all
vars:
myfile: "~/example.txt"
tasks:
- name: Creating an empty file
ansible.builtin.file:
path: "{{ myfile }}"
state: touch
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/create%20file)
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
```yaml
- name: Create config file with proper permissions
ansible.builtin.file:
path: /etc/myapp/config.yml
state: touch
owner: appuser
group: appuser
mode: '0640'
become: true
```
Create file only if it doesn't exist
The `touch` state always updates the timestamp. To create only if missing:
```yaml
- name: Check if file exists
ansible.builtin.stat:
path: /etc/myapp/config.yml
register: config_file
- name: Create file only if missing
ansible.builtin.file:
path: /etc/myapp/config.yml
state: touch
mode: '0644'
when: not config_file.stat.exists
```
Create multiple files
```yaml
- name: Create multiple empty files
ansible.builtin.file:
path: "{{ item }}"
state: touch
mode: '0644'
loop:
- /tmp/file1.txt
- /tmp/file2.txt
- /tmp/file3.log
```
Create a file with content (use copy module instead)
If you need content in the file, use `ansible.builtin.copy`:
```yaml
- name: Create file with content
ansible.builtin.copy:
content: |
# Configuration file
setting1: value1
setting2: value2
dest: /etc/myapp/config.yml
owner: root
group: root
mode: '0644'
become: true
```
Create parent directories automatically
The `file` module with `st
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: installation
Read the full written article: Ansible file Module: Create Files, Directories & Manage Permissions
Related Video Tutorials
- Ansible Create Symlink: Symbolic Links with file Module (Guide) — Discover how to create symlinks with Ansible's file module in this easy-to-follow Playbook. Master Ansible automation with practical examples.
- Ansible Create File: copy Module for Text Files (Complete Guide) — How to create text files with Ansible copy module. Write content inline, copy from controller, set permissions, and manage config files with examples.
- Ansible Rename File: Move & Rename Files with command, copy & file — How to rename files and directories with Ansible. Use command mv, copy+file, and other strategies for moving files on remote hosts with examples.
- Ansible Check If Directory Exists: stat Module Guide — How to check if files and directories exist with Ansible stat module. Use stat results in conditionals, check permissions, size, and timestamps.
- Ansible Create Directory: file Module with state=directory Guide — How to create directories with Ansible file module. Set permissions, ownership, create recursive paths, manage directory structures with examples.
- Ansible Create Hard Link & Symlink: file Module Guide — How to create hard links and symbolic links with Ansible file module. Use state=hard and state=link with permissions, force, and practical examples.