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 win_file Module: Create & Manage Files on Windows (Guide) — Video Tutorial
How to create and manage files on Windows with Ansible win_file module (ansible.windows.win_file). Create files, directories, symlinks on Windows. Practical YAML playbook examples.
What You'll Learn
- How to create an empty file in Windows-like systems with Ansible?
- Ansible creates an empty file
- Parameters
- Links
- Playbook
- code
- execution
- idempotency not valid
- before execution
- after execution
Full Tutorial Content
How to create an empty file in Windows-like systems 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 creates an empty file
- `ansible.windows.win_file`
- Creates, touches, or removes files or directories
Today we're talking about the Ansible module `win_file`.
The full name is `ansible.windows.win_file`, which means that is part of the collection of modules to interact with windows machines.
It's a module pretty stable and out for years.
It creates, touches, or removes files or directories.
For Linux targets, use [the ansible.builtin.file module](/articles/create-an-empty-file-ansible-module-file)) instead.
Parameters
- path path - file path
- state string - file/absent/directory/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.
Links
- [ansible.windows.win_file](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_file_module.html)
Playbook
Create an empty file in Windows-like systems with Ansible playbook
code
```yaml
---
- name: win_file module demo
hosts: all
become: false
gather_facts: false
vars:
myfile: 'C:\Users\vagrant\Desktop\example.txt'
tasks:
- name: Creating an empty file
ansible.windows.win_file:
path: "{{ myfile }}"
state: touch
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ file/file-windows.yml
PLAY [win_file module demo] ***********************************************************************
TASK [Creating an empty file] *********************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency not valid
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory create\ file/file-windows.yml
PLAY [win_file module demo] ***********************************************************************
TASK [Creating an empty file] *********************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
before execution

after execution

[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot)
Conclus
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 2 min
- Category: troubleshooting
Read the full written article: Ansible win_file Module: Create & Manage Files on Windows (Guide)
Related Video Tutorials
- Ansible win_file Module: Create Directory on Windows Hosts (Guide) — How to create directories on Windows with Ansible win_file module (ansible.windows.win_file). Set paths, manage folders, handle permissions. Practical YAML playbook examples for Windows automation.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- Add Windows Registry on Windows-like systems - Ansible module win_regedit — Learn how to use Ansible win_regedit module to add, change, or remove Windows registry key-values efficiently and accurately with simple Ansible code examples.
- Check .NET Framework Version on Windows with Ansible — Learn how to check the .NET Framework version on Windows systems with Ansible using the win_reg_stat module. Includes a practical Playbook example and execution results.
- Ansible win_stat: Check if File or Directory Exists on Windows (Examples) — How to check if a file or directory exists on Windows using Ansible win_stat module. Conditional tasks, file properties, checksum verification with practical examples.
- Ansible Check if File Exists: stat Module with when Conditional (Guide) — How to check if a file exists in Ansible with the stat module and when conditional. Verify file existence, check directories, test before actions. Practical YAML playbook examples.