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 Directory Exists: stat Module Guide — Video Tutorial
How to check if files and directories exist with Ansible stat module. Use stat results in conditionals, check permissions, size, and timestamps.
What You'll Learn
- How to check if a directory exists in Ansible?
- Ansible check directory exists
- Main parameters and return values
- Demo
- code
- Conclusion
- How It Works
- Complete Examples
- Check directory and create if missing
- Check multiple directories
Full Tutorial Content
How to check if a directory 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 directory 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-directory-exists-on-windows-like-systems-ansible-module-win-stat) module instead.
Main parameters and return values
- `path` _string_
The only mandatory parameter is `“path”` which is the filesystem full path of the object to check.
* `stat` _complex_
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.
Demo
Let’s jump in a real-life playbook to check if a directory exists with Ansible.
code
- directory_check_exists.yml
```yaml
---
- name: check if the directory exists
hosts: all
become: false
vars:
directory: "/tmp"
tasks:
- name: Check if the directory exists
ansible.builtin.stat:
path: "{{ directory }}"
register: dir_to_check
- name: Directory found
ansible.builtin.debug:
msg: "Directory {{ directory }} present"
when: dir_to_check.stat.isdir is defined and dir_to_check.stat.isdir
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/check%20directory%20exists)
Conclusion
Now you know better the Ansible module stat and you could use it successfully in your playbook.
How It Works
The `ansible.builtin.stat` module retrieves file or directory metadata without modifying anything. Combined with `register` and `when`, you can create conditional logic based on filesystem state.
Complete Examples
Check directory and create if missing
```yaml
---
- name: Ensure directory exists
hosts: all
tasks:
- name: Check if /opt/myapp exists
ansible.builtin.stat:
path: /opt/myapp
register: myapp_dir
- name: Create directory if not present
ansible.builtin.file:
path: /opt/myapp
state: directory
owner: appuser
group: appuser
mode: '0755'
become: true
when: not myapp_dir.stat.exists
- name: Show directory info
ansible.builtin.debug:
msg: "Directory exists: {{ myapp_dir.stat.exists }}, isdir: {{ myapp_dir.stat.isdir | default(false) }}"
```
Check multiple directories
```yaml
- name: Verify required directories exist
ansible.builtin.stat:
path: "{{ item }}"
register: dir_checks
loop:
- /opt/myapp
- /var/log/myapp
- /etc/myapp
- name: Report missing directories
ansible.builtin.debug:
msg: "MISSING: {{ item.item }}"
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: troubleshooting
Read the full written article: Ansible Check If Directory Exists: stat Module Guide
Related Video Tutorials
- Ansible Create Directory: file Module with state=directory (Guide) — How to create directories in Ansible with the file module state=directory. Set permissions, owner, group, recursive creation.
- 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.
- Ansible Rename File: Move & Rename Files with copy + file Modules — How to rename files and directories in Ansible using copy and file modules. Move files, rename with register, use command module.
- Add Secondary Groups to Linux Users with Ansible Playbook — Learn how to add secondary groups to Linux users with an Ansible playbook. This step-by-step guide includes YAML configuration and execution details.