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_stat: Check if File or Directory Exists on Windows (Examples) — Video Tutorial
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.
What You'll Learn
- How to check if a directory/folder exists on Windows-like systems with Ansible?
- Ansible check directory exists on Windows-like systems
- Parameters & Return Values
- Mandatory Parameters
- Main Return Values
- Links
- code
- directory doesn't exist execution
- directory exist execution
- Conclusion
Full Tutorial Content
How to check if a directory/folder 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 directory exists on Windows-like systems
- `ansible.windows.win_stat`
- Get information about Windows files
Let's talk 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-directory-exists-ansible-module-stat) module instead.
Parameters & Return Values
Mandatory Parameters
- path string
Main Return Values
- stat complex - isdir
The only mandatory parameter is "path" which is the filesystem full path of the object to check.
The module returns a complex object, the property that is interesting for us is "isdir". This attribute is "true" if the object is a directory
Links
- [ansible.windows.win_stat](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_stat_module.html)
## Playbook
How to check if the "example" directory/folder exists on the Desktop of the user on Windows-like systems with Ansible Playbook.
code
```yaml
---
- name: check if is a directory
hosts: all
vars:
directory: 'C:\Users\vagrant\Desktop\example'
tasks:
- name: Check the directory
ansible.windows.win_stat:
path: "{{ directory }}"
register: dir_data
- name: Directory found
ansible.builtin.debug:
msg: "Directory {{ directory }} present"
when: dir_data.stat.isdir is defined and dir_data.stat.isdir
```
directory doesn't exist execution

```bash
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ directory\ exists/directory_exists_windows.yml
PLAY [check if is a directory] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [Check the directory] ************************************************************************
ok: [WindowsServer]
TASK [Directory found] ****************************************************************************
skipping: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
ansible-pilot $
```
directory exist execution

```bash
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory check\ directory\ exist
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 4 min
- Category: installation
Read the full written article: Ansible win_stat: Check if File or Directory Exists on Windows (Examples)
Related Video Tutorials
- Ansible Check If Directory Exists: stat Module Guide — How to check if files and directories exist with Ansible stat module. Use stat results in conditionals, check permissions, size, and timestamps.
- 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.
- Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy) — How to copy files to Windows remote hosts with Ansible win_copy module (ansible.windows.win_copy). Transfer files, directories, set permissions. Practical YAML playbook examples.
- 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 win_file Module: Create & Manage Files on Windows (Guide) — 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.
- 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.