如何删除*.web文件只有它们存在

mik*_*iki 8 ansible ansible-playbook

我需要创建一个Ansible playbook,*.web只有在文件存在的情况下才能删除特定目录中的文件.

操作系统:分OS,Redhat 5x,6x.

我试过以下但没有成功:

 - stat: path=/opt/app/jboss/configuration/*.web
   register: web
 - shell: rm -rf /opt/app/jboss/configuration/*.web
   when: web.stat.exists
Run Code Online (Sandbox Code Playgroud)

小智 17

@ bruce -p的回答给出了Ansible 2.0+的弃用警告,但是新的with_fileglob提供了另一个选项:

- file: path={{ item }} state=absent
  with_fileglob: /opt/app/jboss/configuration/*.web
Run Code Online (Sandbox Code Playgroud)

(类似问题删除目录中包含特定名称的所有文件都有类似的答案.)

编辑:如下所述,这将无效; 这是一个"花哨的方式"的例子:

- find:
    paths: /opt/app/jboss/configuration
    patterns: "*.web"
  register: find_results

- file:
    path: "{{ item['path'] }}"
    state: absent
  with_items: "{{ find_results['files'] }}"
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用,因为`with_fileglob`只匹配运行ansible而不是目标主机的系统上的文件. (3认同)

udo*_*dan 2

rm -rf不关心文件是否存在。它不会抱怨。如果文件存在,它们将被删除。如果没有,那么就没有。但结果是相同的:相同的状态代码,没有输出。那么就不需要在 Ansible 级别上处理这个问题了。