我必须丢失一些东西,但假设我想从http:// location下载一些文件。它失败,如果文件丢失,我想退出剧本运行,如果文件丢失,不会失败。 例子那样,不会对HTTP位于文件Ansible,检查文件存在于get_url/win_get_url之前的http路径
- stat: path=/usr/local/foo
register: foo_var
- meta: end_play
when: foo_var.stat.exists
我必须丢失一些东西,但假设我想从http:// location下载一些文件。它失败,如果文件丢失,我想退出剧本运行,如果文件丢失,不会失败。 例子那样,不会对HTTP位于文件Ansible,检查文件存在于get_url/win_get_url之前的http路径
- stat: path=/usr/local/foo
register: foo_var
- meta: end_play
when: foo_var.stat.exists
虽然你的问题缺乏一些必要的信息工作,我想你看的是这样的:
---
- hosts: localhost
gather_facts: no
tasks:
# check url with HEAD request
- uri:
url: http://localhost:8000/key.pub
method: HEAD
register: uri_test
# fail with error if status is unexpected
failed_when: uri_test.status is undefined or uri_test.status <= 0
# gracefully end play if http code is fatal
- meta: end_play
when: uri_test.status > 400
# else download
- get_url:
url: http://localhost:8000/key.pub
dest: /tmp/key.pub
实际上,你可以跳过uri
测试并只使用get_url
与ignore_errors: yes
或精心制作的failed_when
声明。
谢谢,现在我使用Linux和Windows主机的一个剧本的问题。有没有办法在两个操作系统中使用此代码? 我不想做案件“何时”,并做两次相同的代码。一旦uri然后win_uri –
要使用来自不同调用的相同名称的已注册变量,您必须跳过一些环节(请参阅[这里](http://stackoverflow.com/a/41335880/2795592))。 –
“*如果文件丢失,则失败*” - 缺少位置?在指定的网址?此外,你应该包含[MCVE](https://stackoverflow.com/help/mcve)和错误信息 - 缺失。 – techraf