Check if a file exists on Windows-like systems - Ansible module win_stat
How to automate the checking of the “example.txt” file on the Desktop of the user in the Windows system and display a message or execute a task accordingly with Ansible Playbook.


How to Check if a file exists on 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 check file exists on Windows-like systems
ansible.windows.win_stat
- Get information about Windows files
Today we’re talking about the Ansible module win_stat
.
The full name is ansible.windows.win_stat
, which means that is part of the collection of modules specialized to interact with Windows target host.
It’s a module pretty stable and out for years.
It works in Windows and Windows Server operating systems.
It gets information about Windows files.
For Linux target use the ansible.builtin.stat
module instead.
Parameters & Return Values
Mandatory Parameters
- path string
Main Return Values
- stat complex - exists
The only mandatory parameter is “path” which is the filesystem full path of the object to check. You could also retrieve the checksum of the file in the most popular hash algorithm, just in case. The module returns a complex object, the property that is interesting for us is “exists”. This attribute is “true” if the object exists.
Links
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
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 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
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to check if the file “example.txt” exists on the Desktop of the user on Windows-like systems with Ansible Playbook.
code
---
- name: check if a file exist
hosts: all
vars:
myfile: 'C:\Users\vagrant\Desktop\example.txt'
tasks:
- name: check if a file exist
ansible.windows.win_stat:
path: "{{ myfile }}"
register: file_data
- name: report file exist
ansible.builtin.debug:
msg: "The file {{ myfile }} exist"
when: file_data.stat.exists
- name: report file not exist
ansible.builtin.debug:
msg: "The file {{ myfile }} doesn't exist"
when: not file_data.stat.exists
file doesn’t exist execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ file\ exists/file_exist_windows.yml
PLAY [check if a file exist] **********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [check if a file exist] **********************************************************************
ok: [WindowsServer]
TASK [report file exist] **************************************************************************
skipping: [WindowsServer]
TASK [report file not exist] **********************************************************************
ok: [WindowsServer] => {
"msg": "The file C:\\Users\\vagrant\\Desktop\\example.txt doesn't exist"
}
PLAY RECAP ****************************************************************************************
WindowsServer : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
ansible-pilot $
file exist execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ file\ exists/file_exist_windows.yml
PLAY [check if a file exist] **********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [check if a file exist] **********************************************************************
ok: [WindowsServer]
TASK [report file exist] **************************************************************************
ok: [WindowsServer] => {
"msg": "The file C:\\Users\\vagrant\\Desktop\\example.txt exist"
}
TASK [report file not exist] **********************************************************************
skipping: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
ansible-pilot $
Recap
Now you know how to check if a file exists on 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