What you'll learn
- How to create an empty file with Ansible?
- Ansible create an empty file — ansible.builtin.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)
- Create parent directories automatically
How to create an empty file with Ansible?
Use `ansible.builtin.file` with `state: touch` to create an empty file on a remote host — identical to the Linux `touch` command. If the file already exists, its content is preserved and only the timestamps are updated. Combine with `mode`, `owner`, and `group` to set permissions in the same task.
Ansible create an empty file — ansible.builtin.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 par