如何检查文件是否已在ansible中下载 [英] How can I check if file has been downloaded in ansible

查看:33
本文介绍了如何检查文件是否已在ansible中下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wget 从 ansible 下载文件.

I am downloading the file with wget from ansible.

  - name: Download Solr
    shell: wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip
      args:
        chdir: {{project_root}}/solr 

但我只想在该位置不存在 zip 文件时执行此操作.目前系统每次都在下载.

but I only want to do that if zip file does not exist in that location. Currently the system is downloading it every time.

推荐答案

除非你有使用 wget 的理由,否则为什么不使用 get_url 模块.它将检查文件是否需要下载.

Unless you have a reason to use wget why not use get_url module. It will check if the file needs to be downloaded.

---
- hosts        : all
  gather_facts : no
  tasks:
   - get_url:
       url="http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip"
       dest="{{project_root}}/solr-4.7.0.zip"

注意:如果您将目录而不是完整路径放在 dest 中,ansible 仍会将文件下载到临时目录,但会执行 md5 检查以确定是否复制到目标目录.

NOTE: If you put the directory and not the full path in the dest ansible will still download the file to a temporary dir but do an md5 check to decide whether to copy to the dest dir.

如果您需要保存下载状态,可以使用:

And if you need to save state of download you can use:

---
- hosts        : all
  gather_facts : no
  tasks:
   - get_url:
       url="http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip"
       dest="{{project_root}}/solr-4.7.0.zip"
     register: get_solr

   - debug: 
       msg="solr was downloaded"
     when: get_solr|changed

这篇关于如何检查文件是否已在ansible中下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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