How can I check if file has been downloaded in ans

2019-01-21 23:21发布

I am downloading the file with wget from ansible.

  - name: Download Solr
    shell: chdir={{project_root}}/solr wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.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.

8条回答
贪生不怕死
2楼-- · 2019-01-21 23:52

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"

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
查看更多
何必那么认真
3楼-- · 2019-01-21 23:53

my favourite is to only download the file if it is newer than the local file (which includes when the local file does not exist)

the -N option with wget does this: https://www.gnu.org/software/wget/manual/html_node/Time_002dStamping-Usage.html .

sadly, i don't think there is an equivalent feature in get_url

so a very small change:

- name: Download Solr shell: chdir={{project_root}}/solr wget -N http://<SNIPPED>/solr-4.7.0.zip

查看更多
登录 后发表回答