Delete file or directory - Ansible module file
A real-life example about how the "deleteme" file and the "deleteme" directory with Ansible. Tested in a target Linux machine as well for idempotency of the Ansible Playbook.


How to Delete files or directory with 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 delete file/directory
Today we’re talking about the Ansible module file.
The full name is ansible.builtin.file
, 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 manages files and file properties.
For Windows targets, use the ansible.windows.win_file
module instead.
Main Parameters
- path string (dest, name) - file path
- owner string - user
- group string - group
- mode raw - Ex: ‘0644’ or ‘u=rw,g=r,o=r’
- state string - file/absent/directory/hard/link/touch
- setype/seuser/selevel - SELinux
This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The parameter “owner” sets the user that should own the file/directory. The parameter “group” sets the group that should own the file/directory. The parameter “mode” sets the permissions in the UNIX way of the file/directory. The state defines the type of object we are modifying, the default is “file” but we could also handle directories, hardlink, symlink, or only update the access time with the “touch” option. For our use case, we are going to use the “absent” option. Let me also highlight that we could also specify the SELinux properties.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
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 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
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Let’s jump into a real-life playbook on how to delete files or directories with Ansible.
- file.yml
---
- name: file module demo
hosts: all
vars:
mypath: "/home/devops/deleteme"
become: false
tasks:
- name: "{{ mypath }}" not present
ansible.builtin.file:
path: "{{ mypath }}"
state: "absent"
Recap
Now you know better the Ansible module win_chocolatey and how you could use it successfully in your playbook to automate your day-to-day activities. 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