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)
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)
iGE*_*GEL 16
许多模块已经知道结果,如果它已经存在,将被跳过,例如file或geturl.其他人喜欢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)
这里至少有两个选择.
如果文件存在,您可以注册变量,然后在文件尚不存在的情况下使用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)
这篇文章可能有用
从中得出了这个例子:
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)
| 归档时间: |
|
| 查看次数: |
32780 次 |
| 最近记录: |