我必須丟失一些東西,但假設我想從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