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.

Watch Video

Watch "Ansible file Module: Create Files, Directories & Manage Permissions" on YouTube

What You'll Learn

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

Read the full written article: Ansible file Module: Create Files, Directories & Manage Permissions

Topics Covered

Related Video Tutorials