下面的代码只删除它在web目录中获得的第一个文件。我想删除web目录中的所有文件和文件夹,并保留web目录。我该怎么做呢?
- name: remove web dir contents
file: path='/home/mydata/web/{{ item }}' state=absent
with_fileglob:
- /home/mydata/web/*注意:我已经使用命令和外壳尝试过rm -rf,但它们都不起作用。也许我用错了。
任何在正确方向上的帮助都将不胜感激。
我使用的是ansible 2.1.0.0
发布于 2018-07-31 14:51:00
我真的不喜欢rm解决方案,ansible也给了你关于使用rm的警告。因此,这里是如何在不需要rm和没有ansible警告的情况下完成它的。
- 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 }}"来源:http://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/
发布于 2016-07-05 20:04:01
尝试下面的命令,它应该可以工作
- shell: ls -1 /some/dir
register: contents
- file: path=/some/dir/{{ item }} state=absent
with_items: {{ contents.stdout_lines }}发布于 2018-06-28 16:04:26
根据所有评论和建议创建了全面的重新调整和故障保护实施:
# collect stats about the dir
- name: check directory exists
stat:
path: '{{ directory_path }}'
register: dir_to_delete
# delete directory if condition is true
- name: purge {{directory_path}}
file:
state: absent
path: '{{ directory_path }}'
when: dir_to_delete.stat.exists and dir_to_delete.stat.isdir
# create directory if deleted (or if it didn't exist at all)
- name: create directory again
file:
state: directory
path: '{{ directory_path }}'
when: dir_to_delete is defined or dir_to_delete.stat.exist == Falsehttps://stackoverflow.com/questions/38200732
复制相似问题