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

查看:158
本文介绍了如何检查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天全站免登陆