How to Delete Only Files Inside a Directory with Ansible?

I’m going to show you a live Playbook with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible How to Delete Only Files Inside a Directory

  • ansible.builtin.find
  • Return a list of files based on specific criteria

Today we’re talking about the Ansible module find. The full name is ansible.builtin.find, which means that is part of the collection included in the ansible-core builtin collection. This module returns a list of files based on specific criteria using the find popular Unix command.

Parameters

  • paths string - List of paths of directories to search
  • hidden boolean - no/yes
  • recurse boolean - recursively descend into the directory looking for files
  • file_type string - file/directory/any/link

The most important parameters of the find module for this use case. The mandatory parameter paths specify the list of paths of directories to search. You could include hidden files with the hidden parameter. As well as recurse in any directory under the main path with the recurse parameter. Another useful parameter is file_type, defaults to file but you could filter for directory, link or any filesystem object type.

Playbook

How to delete Ooly files inside a directory with Ansible Playbook. I’m going to delete only the files and directories under the example folder of my login user (devops).

code

---
- name: delete file(s) in dir Playbook
  hosts: all
  vars:
    mypath: "/home/devops/example"
  tasks:
    - name: collect files
      ansible.builtin.find:
        paths: "{{ mypath }}"
        hidden: true
        recurse: true
        file_type: any
      register: collected_files
    - name: remove collected files
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: absent
      with_items: "{{ collected_files }}"

execution

$ ansible-playbook -i virtualmachines/demo/inventory delete\ file\ or\ directory/file_onlyfiles.yml
PLAY [delete file(s) in dir Playbook] *****************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [collect files] ******************************************************************************
ok: [demo.example.com]
TASK [remove collected files and directories] *****************************************************
changed: [demo.example.com] => (item={'path': '/home/devops/example/file1.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1001, 'gid': 10, 'size': 0, 'inode': 484310, 'dev': 64768, 'nlink': 1, 'atime': 1652197335.6893907, 'mtime': 1652197335.6893907, 'ctime': 1652197335.6893907, 'gr_name': 'wheel', 'pw_name': 'devops', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False})
changed: [demo.example.com] => (item={'path': '/home/devops/example/file2.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1001, 'gid': 10, 'size': 0, 'inode': 484311, 'dev': 64768, 'nlink': 1, 'atime': 1652197337.6393802, 'mtime': 1652197337.6393802, 'ctime': 1652197337.6393802, 'gr_name': 'wheel', 'pw_name': 'devops', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False})
changed: [demo.example.com] => (item={'path': '/home/devops/example/deleteme', 'mode': '0755', 'isdir': True, 'ischr': False, 'isblk': False, 'isreg': False, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1001, 'gid': 10, 'size': 23, 'inode': 201823954, 'dev': 64768, 'nlink': 2, 'atime': 1652197359.8632617, 'mtime': 1652197356.4072802, 'ctime': 1652197356.4072802, 'gr_name': 'wheel', 'pw_name': 'devops', 'wusr': True, 'rusr': True, 'xusr': True, 'wgrp': False, 'rgrp': True, 'xgrp': True, 'woth': False, 'roth': True, 'xoth': True, 'isuid': False, 'isgid': False})
ok: [demo.example.com] => (item={'path': '/home/devops/example/deleteme/file3.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1001, 'gid': 10, 'size': 0, 'inode': 201823955, 'dev': 64768, 'nlink': 1, 'atime': 1652197356.4072802, 'mtime': 1652197356.4072802, 'ctime': 1652197356.4072802, 'gr_name': 'wheel', 'pw_name': 'devops', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False})
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
idempotency
$ ansible-playbook -i virtualmachines/demo/inventory delete\ file\ or\ directory/file_onlyfiles.yml
PLAY [delete file(s) in dir Playbook] *****************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [collect files] ******************************************************************************
ok: [demo.example.com]
TASK [remove collected files and directories] *****************************************************
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

before execution

$ ssh [email protected]
[devops@demo ~]$ tree example/
example/
|-- deleteme
|   `-- file3.txt
|-- file1.txt
`-- file2.txt
1 directory, 3 files

after execution

$ ssh [email protected]
[devops@demo ~]$ tree example/
example/
0 directories, 0 files

code with ❤️ in GitHub

Conclusion

Now you know how to delete only files inside a directory with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza