0

我有一个 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 运行此处理程序?

4

2 回答 2

1

引用处理程序和失败章节:

您可以使用命令行选项更改此行为--force-handlers,或者通过force_handlers: True在 play 中或force_handlers = Trueansible.cfg. 强制处理程序时,即使该主机上的任务失败,它们也会在收到通知时运行。(请注意,某些错误仍可能阻止处理程序运行,例如主机无​​法访问。)

于 2017-02-15T02:53:55.733 回答
-3
    ---
    # 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
于 2017-07-28T10:09:17.240 回答