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

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

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.

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

Linksansible.windows.win_stat

## Playbook

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

code

file doesn't exist execution

!win_stat file doesn't exist

file exist execution

!win_stat file exist

code with ❤️ in GitHub

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

Related ArticlesAnsible when Conditional GuideAnsible Inventory GuideAnsible for Windows Guide

Category: troubleshooting

Watch the video: Verify File Existence on Windows with Ansible — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home