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
In fact, this is very simple. We add an a tag to ...
The most important interactive design article in ...
Table of contents 1. Analyzing MySQL from a macro...
Links to the current page. ------------------- Com...
1. Do a good job of cleaning before installation ...
Environment Preparation 1. Environment Constructi...
Cooper talked about the user's visual path, w...
Table of contents splice() Method join() Method r...
html Copy code The code is as follows: <SPAN cl...
Preface Let's get straight to the point. The ...
1 Problem Description Vue3's combined API can...
Table of contents 1. Prototype mode Example 1 Exa...
I used vue and bootstrap to make a relatively sim...
Preface gdb is a very useful debugging tool under...
I believe that everyone needs to copy and paste d...