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

use*_*660 25 ansible ansible-playbook

wget从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
Run Code Online (Sandbox Code Playgroud)

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

pfa*_*con 52

注意:这个答案涵盖了"如何检查文件是否存在于ansible"的一般问题,而不是下载文件的具体情况.

使用"命令"或"shell"操作的先前答案的问题是它们不能在--check模式下工作.实际上,将跳过第一个操作,接下来将在"when: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
Run Code Online (Sandbox Code Playgroud)

  • 我相信local_action已被弃用,您可以直接使用`stat:path = {{secrets_dir}}/secrets.yml`.这回答了我在这里的问题,谢谢.Imo对象stat寄存器可以使用更多文档(或者我可以用ipython检查它)./ lazyme (3认同)

Dom*_*tro 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"
Run Code Online (Sandbox Code Playgroud)

注意:如果您放置目录而不是destansible中的完整路径仍然会将文件下载到临时目录,但执行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
Run Code Online (Sandbox Code Playgroud)

  • 虽然这是答案肯定是下载文件的最佳方法,但问题是"如何检查文件是否存在于ansible中",而不是"如何在ansible中下载文件".对OP实际需要的内容进行了很好的阅读,但从技术上讲,这并没有回答这个问题. (16认同)
  • DomaNitro:@BenWhaley说你没有回答这个问题是正确的.不要只考虑曾经问过一个问题的单身人士,想想数百万将永远谷歌的人.你的回答只会浪费时间.底线:问题启动者不应该接受你的答案,所以它不是第一排. (4认同)
  • 引用的问题“我只想在该位置不存在 zip 文件时才这样做”,该代码就是这样做的。第二个任务是调试,如果您查看**何时**,它仅在文件不存在且已下载时才会执行。 (2认同)
  • @pfalcon 感谢您的评论,但我不同意提出问题的人对他在某些情况下提出的问题感到满意。因此在 get_url 之后添加另一层检查是多余的。如果您想检查文件是否存在,请再次询问该问题或检查http://docs.ansible.com/file_module.html (2认同)

iGE*_*GEL 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
Run Code Online (Sandbox Code Playgroud)


Ben*_*ley 8

这里至少有两个选择.

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

- 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
Run Code Online (Sandbox Code Playgroud)

您还可以使用带有选项的命令模块creates:

- 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
Run Code Online (Sandbox Code Playgroud)

  • 下载大文件时请使用`wget -nv`或`wget -q`,否则进度条不断输出会导致ansible内存错误。 (2认同)

cze*_*asz 5

这篇文章可能有用

从中得出了这个例子:

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
Run Code Online (Sandbox Code Playgroud)

  • `如果 true 则返回 true,否则返回 false endif` (3认同)