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

How to set up a shared folder on a vmware16 virtual machine

1. Set up a shared folder on the virtual machine:...

Vue mobile terminal determines the direction of finger sliding on the screen

The vue mobile terminal determines the direction ...

Can't connect to local MySQL through socket '/tmp/mysql.sock' solution

Error message: ERROR 2002: Can't connect to l...

MySQL Basic Tutorial: Detailed Explanation of DML Statements

Table of contents DML statements 1. Insert record...

getdata table table data join mysql method

public function json_product_list($where, $order)...

Pure CSS3 to achieve pet chicken example code

I have read a lot of knowledge and articles about...

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...

How to import Tomcat source code into idea

Table of contents 1. Download the tomcat code 2. ...

Development details of Vue3 components

Table of contents 1. Introduction 2. Component De...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Detailed explanation of docker-machine usage

Docker-machine is a Docker management tool offici...

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...

Complete steps of centos cloning linux virtual machine sharing

Preface When a Linux is fully set up, you can use...

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...