Create an empty file in Windows-like systems - Ansible module win_file
How to automate the creation of an empty “example.txt” in the Desktop of your users in Windows-like systems with Ansible Playbook.


How to create an empty file in Windows-like systems with Ansible?
I’m going to show you a live demo 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) 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
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Create an empty file in Windows-like systems with Ansible playbook
code
---
- 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
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
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
Recap
Now you know how to create an empty file in Windows-like systems with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate