2017-01-10 28 views
1

我希望有條件地運行Ansible角色,即僅當某些二進制文件不存在時(對於我來說,這意味着某些特定應用程序安裝不存在)。Ansible中的條件角色包含失敗

類似於使用的圖案here

在我的劇本使用下面的代碼:

- hosts: my_host 

    tasks: 
     - name: check app existence 
     command: /opt/my_app/somebinary 
     register: myapp_exists 
     ignore_errors: yes 

    roles: 
     - { role: myconditional_role, when: myapp_exists|failed } 
     - another_role_to_be_included_either_way 

這裏是輸出:

PLAY *************************************************************************** 

TASK [setup] ******************************************************************* 
ok: [my_host ] 

TASK [ myconditional_role : create temporary installation directory] *********************** 
fatal: [my_host]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check 'myapp_exists|failed' failed. The error was: ERROR! |failed expects a dictionary"} 

爲什麼條件檢查失敗?

Ubuntu 16.04.01

使用ansible 2.0.0.2順便說一句:「創建臨時安裝目錄」是有條件地包括角色的第一主任務的名稱。

回答

2

任務在角色後執行,所以myapp_exists未定義。
改爲使用pre_tasks

另請注意,when實際上並不是一個條件角色,它只是將此when語句附加到您角色中的每個任務。

+0

因此,針對特定用例的任何建議的條件角色包含模式? – pkaramol

+1

我會在角色的main.yml任務文件中移動測試,並在需要時包含install.yml。 –