2017-02-15 74 views
0

我有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運行此處理程序,而不管發生在任務中的情況如何?

回答

1

引述Handlers and Failure章:

可以,或者通過包括在戲劇force_handlers: True,或force_handlers = Trueansible.cfg更改與--force-handlers命令行選項此行爲。當強制處理程序時,即使在該主機上任務失敗時,它們也會在通知時運行。 (請注意,某些錯誤仍然可以防止運行處理程序,如主機不可達變得)

-1
--- 
    # 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 
+2

沒有解釋,沒有任何關係處理和破碎YAML壓痕... –

+1

**從審覈隊列:**我是否可以請求您請您在答案中添加更多的上下文。僅有代碼的答案很難理解。如果您可以在帖子中添加更多信息,它可以幫助提問者和未來的讀者。另請參閱[完全解釋基於代碼的答案](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)。 –

相關問題