AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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)

By Luca Berton · Published 2024-01-01 · Category: installation

How to check if a file or directory exists on Windows using Ansible win_stat module. Conditional tasks, file properties, checksum verification with practical.

Ansible win_stat: Check if File or Directory Exists on Windows (Examples)

How to check if a directory/folder exists on Windows-like systems with Ansible?

See also: Ansible Check If Directory Exists: stat Module Guide

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 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

See also: Ansible win_file Module: Create Directory on Windows Hosts (Guide)

## Playbook

How to check if the "example" directory/folder exists on the Desktop of the user on Windows-like systems with Ansible Playbook.

code

---
- 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

win_stat directory doesn't exist
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

win_stat directory exist
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] ****************************************************************************
ok: [WindowsServer] => {
    "msg": "Directory C:\\Users\\vagrant\\Desktop\\example present"
}
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

code with ❤️ in GitHub

Conclusion

Now you know how to check if a directory exists on Windows-like systems with Ansible.

See also: Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy)

Check Directory Exists

- name: Check if directory exists
  ansible.windows.win_stat:
    path: C:\Program Files\MyApp
  register: dir_result

- name: Create if missing
  ansible.windows.win_file:
    path: C:\Program Files\MyApp
    state: directory
  when: not dir_result.stat.exists

Check File Exists

- name: Check config file
  ansible.windows.win_stat:
    path: C:\MyApp\config.yml
  register: config_check

- name: Deploy default config
  ansible.windows.win_copy:
    src: files/default-config.yml
    dest: C:\MyApp\config.yml
  when: not config_check.stat.exists

Get File Properties

- name: Get file info
  ansible.windows.win_stat:
    path: C:\MyApp\app.exe
  register: file_info

- debug:
    msg:
      - "Size: {{ file_info.stat.size }} bytes"
      - "Modified: {{ file_info.stat.lastwritetime }}"
      - "Is directory: {{ file_info.stat.isdir }}"
      - "Is read-only: {{ file_info.stat.isreadonly }}"

Checksum Verification

- name: Get file checksum
  ansible.windows.win_stat:
    path: C:\Downloads\installer.exe
    checksum_algorithm: sha256
  register: dl_check

- name: Verify integrity
  assert:
    that: dl_check.stat.checksum == expected_checksum
    fail_msg: "File corrupted — checksum mismatch"

Common Patterns

Backup before overwrite

- name: Check if config exists
  ansible.windows.win_stat:
    path: C:\MyApp\config.yml
  register: existing

- name: Backup existing config
  ansible.windows.win_copy:
    src: C:\MyApp\config.yml
    dest: "C:\Backup\config-{{ ansible_date_time.iso8601_basic_short }}.yml"
    remote_src: true
  when: existing.stat.exists

- name: Deploy new config
  ansible.windows.win_copy:
    src: files/config.yml
    dest: C:\MyApp\config.yml

Wait for file to appear

- name: Wait for install to complete
  ansible.windows.win_stat:
    path: C:\MyApp\install_complete.flag
  register: flag
  retries: 30
  delay: 10
  until: flag.stat.exists

win_stat vs stat

ModulePlatformPath Style
win_statWindowsC:\path\to\file
statLinux/macOS/path/to/file

Available Properties

PropertyDescription
existsFile/directory exists
isdirIs a directory
isregIs a regular file
sizeFile size in bytes
checksumFile hash (with algorithm)
isreadonlyRead-only flag
ishiddenHidden flag
lastwritetimeLast modification time

FAQ

How do I check a UNC path?

- win_stat:
    path: \\\\fileserver\\share\\folder
  register: unc_check

Yes by default. Use follow: false to check the link itself.

Category: installation

Watch the video: Ansible win_stat: Check if File or Directory Exists on Windows (Examples) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home