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 new relational database features in MySQL 8.0
>>: How to optimize images to improve website performance
1. Set up a shared folder on the virtual machine:...
The vue mobile terminal determines the direction ...
Error message: ERROR 2002: Can't connect to l...
After reinstalling the system today, I reinstalle...
Table of contents DML statements 1. Insert record...
public function json_product_list($where, $order)...
I have read a lot of knowledge and articles about...
This question originated from a message on Nugget...
Table of contents 1. Download the tomcat code 2. ...
Table of contents 1. Introduction 2. Component De...
Using mask layers in web pages can prevent repeat...
Docker-machine is a Docker management tool offici...
stat function and stat command Explanation of [in...
Preface When a Linux is fully set up, you can use...
Table of contents 1. Control the display and hidi...