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 Check if File Exists: stat Module with when Conditional (Guide) — Video Tutorial
How to check if a file exists in Ansible with the stat module and when conditional. Verify file existence, check directories, test before actions.
What You'll Learn
- How to check if a file exists in Ansible?
- Ansible check file exists
- Mandatory Parameters
- Main Return Values
- code
- Conclusion
- Advanced File Checks
- Check file and take action
- Verify file checksum (integrity check)
- Check file age
Full Tutorial Content
How to check if a file exists in 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
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`](/articles/check-if-a-file-exists-on-windows-like-systems-ansible-module-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.
## Playbook
Let's jump in a real-life playbook to check if a file exists with Ansible.
code
- file_exist.yml
```yaml
---
- 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
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/check%20file%20exists)
Conclusion
Now you know how to check if a file exists with Ansible.
Advanced File Checks
Check file and take action
```yaml
- name: Check if config exists
ansible.builtin.stat:
path: /etc/myapp/config.yml
register: config_file
- name: Copy default config if missing
ansible.builtin.copy:
src: files/default-config.yml
dest: /etc/myapp/config.yml
mode: '0644'
when: not config_file.stat.exists
- name: Read existing config
ansible.builtin.slurp:
src: /etc/myapp/config.yml
register: config_content
when: config_file.stat.exists
```
Verify file checksum (integrity check)
```yaml
- name: Get file checksum
ansible.builtin.stat:
path: /opt/myapp/binary
checksum_algorithm: sha256
register: binary_info
- name: Fail if binary is corrupted
ansible.builtin.fail:
msg: "Binary checksum mismatch! Expected {{ expected_checksum }}"
when:
- binary_info.stat.exists
- binary_info.stat.checksum != expected_checksum
```
Check file age
```yaml
- name: Get file info
ansible.builtin.stat:
path: /var/log/myapp/app.log
register: log_file
- name: Rotate if file is larger than 100MB
ansible.builtin.command: logrotate -f /etc/logrotate.d/myapp
when:
- log
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 7 min
- Category: installation
Read the full written article: Ansible Check if File Exists: stat Module with when Conditional (Guide)
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 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 win_stat: Check if File or Directory Exists on Windows (Examples) — 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 Create Symlink: file Module with state=link (Guide) — How to create symbolic links (symlinks) in Ansible with the file module state=link. Create, manage, remove symlinks and hard links.
- Ansible Create File with Content: copy Module content Parameter — How to create files with content in Ansible using the copy module content parameter. Write text, YAML, JSON to files without templates.
- Ansible Create Empty File: Touch Files with file Module (Guide) — How to create empty files in Ansible with the file module state=touch. Create files, set permissions and ownership on new files.