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.
Verify File Existence on Windows with Ansible — Video Tutorial
Discover how to check if a file exists on Windows systems with Ansible's win_stat module. This guide includes a live Playbook example and execution details.
What You'll Learn
- How to Check if a file exists on Windows-like systems with Ansible?
- Ansible check file exists on Windows-like systems
- Parameters & Return Values
- Mandatory Parameters
- Main Return Values
- Links
- code
- file doesn't exist execution
- file exist execution
- Conclusion
Full Tutorial Content
How to Check if a file exists on 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 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`](/articles/check-if-a-file-exists-ansible-module-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
- [ansible.windows.win_stat](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_stat_module.html)
## Playbook
How to check if the file "example.txt" exists on the Desktop of the user on Windows-like systems with Ansible Playbook.
code
```yaml
---
- 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

```bash
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 ********************************************************
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Verify File Existence on Windows with Ansible
Related Video Tutorials
- 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.
- Ansible stat Module: Check if File or Directory Exists (Complete Guide) — Complete guide to Ansible stat module (ansible.builtin.stat). Check if files and directories exist, get size, permissions, checksums, and ownership.
- Ansible Create Directory: file Module with state=directory Guide — How to create directories with Ansible file module. Set permissions, ownership, create recursive paths, manage directory structures with examples.
- Create Directories in Windows Systems Using Ansible — Master directory creation on Windows systems with Ansible’s win_file module. Follow our live Playbook example for efficient directory management.
- Ansible Create Hard Link & Symlink: file Module Guide — How to create hard links and symbolic links with Ansible file module. Use state=hard and state=link with permissions, force, and practical examples.
- Ansible Create Symlink: Symbolic Links with file Module (Guide) — Discover how to create symlinks with Ansible's file module in this easy-to-follow Playbook. Master Ansible automation with practical examples.