我有an Ansible role其中有the following tasks:無論任務失敗,都可以運行處理程序嗎?
---
# optionally find the latest version of goss using the GitHub "API"
- name: detect latest version
shell: |
curl -sIS https://github.com/aelsabbahy/goss/releases/latest | \
tr -d '\r' | \
grep -oP '(?<=Location:\s).*' | \
grep -oP '(?<=v)\d+\.\d+\.\d+'
register: detected_latest
when: version == "latest"
- name: set detected version
set_fact:
real_version: "{{ detected_latest.stdout.strip() }}"
when: version == "latest"
- name: set specified verison
set_fact:
real_version: "{{ version }}"
when: version != "latest"
# set play facts
- name: set facts
set_fact:
download_url: "https://github.com/aelsabbahy/goss/releases/download/v{{ real_version }}/goss-linux-amd64"
# create goss directories
- name: create goss directories
file: path={{ item }} state=directory
with_items:
- /tmp/degoss
- /tmp/degoss/bin
- /tmp/degoss/tests
notify: clean
# download goss
- name: install
get_url:
url: "{{ download_url }}"
dest: /tmp/degoss/bin/goss
mode: 0755
# deploy test cases
- name: deploy tests
copy: src={{ item }} dest=/tmp/degoss/tests/
with_items: "{{ tests }}"
# run the tests
- name: run tests
goss: executable=/tmp/degoss/bin/goss path="{{ item }}" format="{{ format }}"
with_fileglob: /tmp/degoss/tests/*.yml
也就是說,當create goss directories
運行時,它會觸發the clean
handler:
---
# handlers file for degoss
- name: clean
file: path=/tmp/degoss state=absent
由於我的模塊的性質,我想clean
處理程序始終運行,即使角色中的其他任務失敗。從我粗略的測試中,如果run tests
失敗,處理程序從不會被調用,並且臨時文件留在目標機器上。
有沒有一種方法可以從我的角色中強制Ansible運行此處理程序,而不管發生在任務中的情況如何?
沒有解釋,沒有任何關係處理和破碎YAML壓痕... –
**從審覈隊列:**我是否可以請求您請您在答案中添加更多的上下文。僅有代碼的答案很難理解。如果您可以在帖子中添加更多信息,它可以幫助提問者和未來的讀者。另請參閱[完全解釋基於代碼的答案](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)。 –