如何仅在 *.web 文件存在时删除它们 [英] How to delete *.web files only if they exist

查看:22
本文介绍了如何仅在 *.web 文件存在时删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个 Ansible playbook 来删除特定目录中的 *.web 文件,前提是文件存在.

I need to create an Ansible playbook to delete the *.web files in a specific directory only if the files exists.

操作系统:cent 操作系统、Redhat 5x、6x.

OS : cent OS, Redhat 5x, 6x.

我尝试了以下方法但没有成功:

I have tried the following with no success:

 - stat: path=/opt/app/jboss/configuration/*.web
   register: web
 - shell: rm -rf /opt/app/jboss/configuration/*.web
   when: web.stat.exists

推荐答案

@bruce-p 的回答给出了 Ansible 2.0+ 的弃用警告,但新的 with_fileglob 给出了另一种选择:

@bruce-p's answer gives a deprecation warning with Ansible 2.0+, but the new with_fileglob gives another option:

- file: path={{ item }} state=absent
  with_fileglob: /opt/app/jboss/configuration/*.web

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

(Similar question remove all files containing a certain name within a directory has a similar answer.)

如下所述,这是行不通的;这是奇特方式"的示例:

As noted below, that won't work; here's an example of "the fancy way":

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

- file:
    path: "{{ item['path'] }}"
    state: absent
  with_items: "{{ find_results['files'] }}"

这篇关于如何仅在 *.web 文件存在时删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆