Check if a file exists - Ansible module stat
How to check if a file exists in Ansible? I’m going to show you a live demo and some simple Ansible code. I'm going to show you how to combine the "stat" module with conditional to verify an existing and not existing path.


How to check if a file exists in 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
Today we’re talking about the Ansible module stat
.
The full name is ansible.builtin.stat
, which means that is part of the collection of modules “builtin” with ansible and shipped with it.
It’s a module pretty stable and out for years.
It works in a different variety of operating systems.
It retrieves a file entry or a file system status.
For Windows target use the ansible.windows.win_stat
module instead.
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. The module returns a complex object, the property that is interesting for us is “exists”. This attribute is “true” if the object exists.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
demo
Let’s jump in a real-life playbook to check if a file exists with Ansible.
code
- file_exist.yml
---
- name: check if a file exist
hosts: all
become: false
vars:
myfile: /home/devops/test.txt
tasks:
- name: check if a file exists
ansible.builtin.stat:
path: "{{ myfile }}"
register: file_data
- name: report file exists
ansible.builtin.debug:
msg: "The file {{ myfile }} exist"
when: file_data.stat.exists
- name: report file not exists
ansible.builtin.debug:
msg: "The file {{ myfile }} doesn't exist"
when: not file_data.stat.exists
Recap
Now you know how to check if a file exists 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