我有一个 Ansible 角色,它具有以下任务:
---
# 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
运行时,它会触发处理clean
程序:
---
# handlers file for degoss
- name: clean
file: path=/tmp/degoss state=absent
由于我的模块的性质,我希望clean
处理程序始终运行,即使角色中的其他任务失败。根据我的粗略测试,如果run tests
失败,则永远不会调用处理程序,并且临时文件会留在目标机器上。
无论任务中发生了什么,我的角色是否有办法强制 Ansible 运行此处理程序?