How to delete all contents in a directory using Ansible

How to delete all contents in a directory using Ansible

Students who use Ansible know that Ansible only supports adding and deleting specific folders or files, as shown below:

1. Create a directory and delete the entire directory

- name: Create a directory if it does not exist
 file:
  path: /appvol/some_directory
  state: directory
  mode: '0755'
 
- name: Remove a directory if it exists
 file:
  path: /appvol/some_directory
  state: absent

2. Create files and delete single files

- name: Create a file if it does not exist
 file:
  path: /appvol/some_directory/hello.txt
  state: touch
  mode: '0755'
 
 
- name: Remove a file if it exists
 file:
  path: /appvol/some_directory/hello.txt
  state: absent

In some scenarios, we want to clear the log folder or cache folder. At this time, we only need to delete all the contents in the directory.

3. Delete all files in a directory, or the file names that meet the conditions

#First use the shell module to get all the file names in the directory and store them in a variable files_list
- name: list the files of dir some_directory
 shell: ls
 args:
  chdir: /appvol/some_directory
 register: files_list
 
#Use the with_items attribute to output the files_list variable in the form of lines, and then use the file module to loop and delete each file - name: Remove a directory if it does not exist
 file:
  path: /appvol/some_directory/{{ item }}
  state: absent
 with_items:
  - "{{ files_list.stdout_lines }}"

Refer to the official documentation of Ansible:

Ansible file module reference: refer to https://docs.ansible.com/ansible/latest/modules/file_module.html?highlight=file

Ansible shell module parameters: https://docs.ansible.com/ansible/latest/modules/shell_module.html?highlight=shell

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Ansible, a centralized management platform
  • Installation and use of cluster operation and maintenance automation tool ansible (including module and playbook use)

<<:  Detailed explanation of new relational database features in MySQL 8.0

>>:  How to optimize images to improve website performance

Recommend

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

MySQL 5.6 installation steps with pictures and text

MySQL is an open source small relational database...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

Summary of MySQL lock related knowledge

Locks in MySQL Locks are a means to resolve resou...

How to use less in WeChat applet (optimal method)

Preface I am used to writing less/sass, but now I...

A brief comparison of Props in React

Table of contents Props comparison of class compo...

Sample code for implementing history in vuex

I have recently been developing a visual operatio...

Specific use of useRef in React

I believe that people who have experience with Re...

How to remove the dotted border when clicking a link in FireFox

I encountered several browser compatibility issue...

Why Nginx is better than Apache

Nginx has taken over the majority of the Web serv...

A brief discussion on how to choose and combine div and table

Page layout has always been my concern since I st...