如何检查文件是否已在ansible中下载

时间:2014-03-18 03:36:51

标签: ansible ansible-playbook

我正在从ansible下载wget文件。

  - 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

但如果该位置不存在zip文件,我只想这样做。目前系统每次都在下载它。

8 个答案:

答案 0 :(得分:52)

注意:这个答案涵盖了一般问题"我如何在ansible"中检查文件是否存在,而不是下载文件的特定情况。

使用"命令"以前答案的问题或者" shell"行动是他们不能在--check模式下工作。实际上,第一个动作将被跳过,接下来将错误输出"当:solr_exists.rc!= 0"条件(由于变量未定义)。

自Ansible 1.3以来,有更直接的方法来检查文件存在 - 使用" stat"模块。它当然也适用于" local_action"检查本地文件是否存在:

- local_action: stat path={{secrets_dir}}/secrets.yml
  register: secrets_exist

- fail: msg="Production credentials not found"
  when: secrets_exist.stat.exists == False

答案 1 :(得分:31)

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

---
- 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检查以决定是否要复制到目的地。

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

---
- 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

答案 2 :(得分:16)

许多模块已经知道结果,如果它已经存在,将被跳过,例如filegeturl。像command这样的其他选项有一个creates选项,如果该文件已存在(或者如果使用removes选项则不存在),将跳过此命令。

所以你应该首先检查可用的模块,如果它们已经足够聪明的话。如果不是:我推荐stats模块。优于其他解决方案:没有"红色错误但忽略"在输出中。

- name: Check MySQL data directory existence
  stat: path=/var/lib/mysql-slave
  register: mysql_slave_data_dir

- name: Stop MySQL master to copy data directory
  service: name=mysql state=stopped
  sudo: yes 
  when: not mysql_slave_data_dir.stat.exists

答案 3 :(得分:8)

这里至少有两个选择。

如果文件存在,您可以注册变量,然后use a when condition在文件尚不存在的情况下执行命令:

- command: /usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip
  register: solr_zip
  ignore_errors: True
- name: Download Solr
  shell: chdir={{project_root}}/solr /usr/bin/wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip
  when: solr_zip|failed

您还可以将commands modulecreates选项一起使用:

- name: Download Solr
  command: /usr/bin/wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip chdir={{project_root}}/solr  creates={{project_root}}/solr/solr-4.7.0.zip

答案 4 :(得分:4)

所以基本上你可以通过从命令注册变量并检查其返回码来进行检查。 (您也可以通过检查其标准输出来执行此操作)

- name: playbook
  hosts: all
  user: <your-user>

  vars:
    project_root: /usr/local

  tasks:

    - name: Check if the solr zip exists.
      command: /usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip
      ignore_errors: True
      register: solr_exists

    - 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
      when: solr_exists.rc != 0

这基本上说如果/usr/bin/test -e {{project_root}}/solr/solr-4.7.0.zip命令返回的代码不是0,意味着它不存在,那么执行任务Download Solr

希望它有所帮助。

答案 5 :(得分:4)

This article可能有用

出于这个例子:

tasks:
  - shell: if [[ -f "/etc/monitrc" ]]; then /bin/true; else /bin/false; fi
    register: result
    ignore_errors: True

  - command: /bin/something
    when: result|failed

  - command: /bin/something_else
    when: result|success

  - command: /bin/still/something_else
    when: result|skipped

答案 6 :(得分:0)

使用- name: Download Solr shell: creates={{working_directory}}/solr/solr-4.7.0.zip chdir={{working_directory}}/solr wget http://mirror.mel.bkb.net.au/pub/apache/lucene/solr/4.7.0/solr-4.7.0.zip 参数

{{1}}

答案 7 :(得分:0)

我最喜欢的是仅下载比本地文件新的文件(包括本地文件不存在时)

使用wget的-N选项可以做到:https://www.gnu.org/software/wget/manual/html_node/Time_002dStamping-Usage.html

遗憾的是,我认为get_url中没有等效功能

变化很小:

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