我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运行此处理程序而不管任务中发生了什么?
答案 0 :(得分:1)
引用Handlers and Failure章节:
您可以使用
--force-handlers
命令行选项更改此行为,或在游戏中加入force_handlers: True
,或在force_handlers = True
中加ansible.cfg
。强制处理程序时,即使任务在该主机上失败,它们也会在收到通知时运行。 (请注意,某些错误仍可能阻止处理程序运行,例如主机无法访问。)
答案 1 :(得分:-2)
---
# tasks file for block
- name: command 0
shell: uname -i
- block:
- name: command1
shell: ls /tmp/
- name: command2
shell: ls /tmp/momo
rescue:
- name: retour arriere ya eu une erreur
shell: ls -ls