Ansible:即使使用了區塊救援區塊,include_tasks 也會拋出致命錯誤

Ansible:即使使用了區塊救援區塊,include_tasks 也會拋出致命錯誤

我試圖根據一個主機變數中定義的某些值包含多個任務,但即使我使用區塊救援區塊,也會引發一些致命錯誤。我有變數簡介定義於host_vars/主機名稱.yml:

profiles: '["profile1","trap1"]'

和角色測試設定檔/etc/ansible/角色。在這裡,在任務目錄中我有以下 .yml 檔案:設定檔1.yml,設定檔2.yml,主.yml

main.yml文件的內容是:

- name: import profiles
   block:
     - include_tasks: "{{ item }}.yml"
       with_items: "{{ profiles|default([]) }}"
   rescue:
     - debug: msg='Error encountered while trying to include a .yml file corresponding to a defined profile from profiles variable. Profiles - "{{ profiles }}"'
   when: profiles is defined

Playbook的內容是:

 - name: Test profiles config
   hosts: myhost
   roles:
     - test_profiles

輸出是這樣的:

TASK [test_profiles : include_tasks] ***********************************************************************************************************************************************************************
included: /etc/ansible/roles/test_profiles/tasks/profile.yml for <my_host>
fatal: [<my_host>]: FAILED! => {"reason": "Unable to retrieve file contents\nCould not find or access '/etc/ansible/trap1.yml'"}

TASK [test_profiles : Ensure that profile directory exists into the D:\profiles directory] ************************************************************************************************
ok: [<my_host>]

TASK [test_profiles : Update profile.properties file] ***************************************************************************************************************************************************
ok: [<my_host>]

TASK [test_profiles : debug] *******************************************************************************************************************************************************************************
ok: [<my_host>] => {
    "msg": "Error encountered while trying to include a .yml file corresponding to a defined profile from profiles variable. Profiles - \"[\"profile1\",\"trap1\"]\""
}
        to retry, use: --limit @/etc/ansible/playbook_test_profiles.retry

PLAY RECAP ********************************************************************************************************************************************************************************************************
<my_host>                 : ok=5    changed=0    unreachable=0    failed=1

從我的角度來看,致命錯誤不應該出現。我在這裡做錯了什麼以及如何擺脫這個問題?我也嘗試過什麼時候有條件但沒有成功。

我在用著安塞布爾2.4.2.0而這些任務是針對某些 Windows 主機執行的。

答案1

block/rescue並不能阻止錯誤的發生。它檢測失敗的任務並執行rescue區塊以從錯誤中恢復。但失敗的任務仍然存在,並將在遊戲回顧中可見。

我建議在設計劇本時使用快速失敗方法。在您的情況下,您可以掃描本機檔案來測試使用者輸入(提供的設定)是否有效:設定檔是否就位。使用者assert/fail模組。

答案2

最後我刪除了阻止救援塊。我找到了一種方法來檢查我的 .yml 檔案是否存在。這是使用角色路徑變數(它將返回當前角色的路徑 - 自 Ansible 1.8 起可用 - 這只適用於角色內部)和測試是文件

上述角色的 main.yml 如下圖所示:

- name: Import profiles
   include_tasks: "{{ item }}.yml"
   with_items: "{{ profiles|default([]) }}"
   when: (role_path + '/tasks/' + item + '.yml') | is_file

由於此檢查,將不再拋出致命異常 - trap1.yml 檔案將被跳過。

輸出將類似:

TASK [test_profiles : Import profiles] ***********************************************************
skipping: [<my_host>] => (item=trap1)
included: /etc/ansible/roles/test_profiles/tasks/profile1.yml for <my_host>

TASK [test_profiles : Ensure that profile directory exists into the D:\profiles directory] *******
ok: [<my_host>]

TASK [test_profiles : Update profile.properties file] ********************************************
changed: [<my_host>]
        to retry, use: --limit @/etc/ansible/playbook_test_profiles.retry

PLAY RECAP ***************************************************************************************
<my_host>                 : ok=4    changed=1    unreachable=0    failed=0

我同意這個解決方案,但我也願意接受其他一些建議。

相關內容