# ansible-doc yum # 创建目录,删除目录(包括目录下的所有文件) - name: Create a directory if it does not exist file: path: /appvol/some_directory state: directory mode: '0755' - name: Remove a directory if it exist file: path: /appvol/some_directory state: absent # 创建文件,删除文件(单个文件删除) - 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 exist file: path: /appvol/some_directory/hello.txt state: absent # 删除某个目录下的所有文件,或者符合条件的文件名 - name: list the files of dir some_directory shell: ls args: chdir: /appvol/some_directory register: files_list - name: Remove a directory if it does not exist file: path: /appvol/some_directory/{{ item }} state: absent with_items: - "{{ files_list.stdout_lines }}" # 移除晚于某个日期的文件 - hosts: all tasks: - name: Ansible delete files older than 5 days example find: paths: /Users/dnpmacpro/Documents/Ansible age: 5d register: files_to_delete - name: Ansible remove files older than a date example file: path: "{{ item.path }}" state: absent with_items: "{{ files_to_delete.files }}" # 使用find和file模块结合python的正则表达式删除文件 - hosts: all tasks: - name: Ansible delete file wildcard find: paths: /etc/wild_card/example patterns: "^he.*.txt" use:regex: true register: wildcard_files_to_delete - name: Ansible remove file wildcard file: path: "{{ item.path }}" state: absent with_items: "{{ wildcard_files_to_delete.files }}" # 使用find和file模块结合linux shell模糊搜索删除文件 - hosts: all tasks: - name: Ansible delete file glob find: paths: /etc/Ansible patterns: *.txt register: files_to_delete - name: Ansible remove file glob file: path: "{{ item.path }}" state: absent with_items: "{{ files_to_delete.files }}" # 判断文件是否存在 - name: "check that the iptables.modules exists" stat: path: /etc/sysconfig/modules/iptables.modules register: file_status - name: check that if the file devnet.md exists debug: msg: "File exists." when: file_status.stat.exists == True - name: Check that if the file devnet.md not exists debug: msg: "File not found." when: file_status.stat.exists == False - name: Create the file, if it doesnt exist already file: path: /root/devnet.md state: touch when: file_status.stat.exists == False - name: "disabled selinux" replace: path: /etc/selinux/config regexp: '^SELINUX=.*$' replace: 'SELINUX=disabled' backup: true