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 pop up a temporary QQ dialog box to chat online without adding friends

In fact, this is very simple. We add an a tag to ...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...

How a select statement is executed in MySQL

Table of contents 1. Analyzing MySQL from a macro...

What does href=# mean in a link?

Links to the current page. ------------------- Com...

Detailed tutorial on how to install mysql8.0 using Linux yum command

1. Do a good job of cleaning before installation ...

React-Native environment setup and basic introduction

Environment Preparation 1. Environment Constructi...

The visual design path of the website should conform to user habits

Cooper talked about the user's visual path, w...

Learn the common methods and techniques in JS arrays and become a master

Table of contents splice() Method join() Method r...

How to click on the a tag to pop up the input file upload dialog box

html Copy code The code is as follows: <SPAN cl...

Index in MySQL

Preface Let's get straight to the point. The ...

WebStorm cannot correctly identify the solution of Vue3 combined API

1 Problem Description Vue3's combined API can...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

Vue+Bootstrap realizes a simple student management system

I used vue and bootstrap to make a relatively sim...

Getting Started Tutorial on GDB in Linux

Preface gdb is a very useful debugging tool under...

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste d...