2016-09-22 19 views
6

我遇到了最愚蠢的問題。 我無法弄清楚如何在Ansible 2.2任務文件中測試布爾值。使用真正的錯誤與Ansible當子句

vars/main.yml,我有:

destroy: false 

在劇本,我有:

roles: 
    - {'role': 'vmdeploy','destroy': true} 

在任務文件中,我有以下幾點:

- include: "create.yml" 
    when: "{{ destroy|bool }} == 'false'" 

我已經嘗試過以下各種組合:

when: "{{ destroy|bool }} == false" 
when: "{{ destroy|bool }} == 'false'" 
when: "{{ destroy|bool == false}}" 
when: "{{ destroy == false}}" 
when: "{{ destroy == 'false'}}" 
when: destroy|bool == false 
when: destroy|bool == 'false' 
when: not destroy|bool 

在上述所有情況下,我仍然得到:

statically included: .../vmdeploy/tasks/create.yml 

調試輸出:

- debug: 
    msg: "{{ destroy }}" 

--- 

ok: [atlcicd009] => { 
"msg": true 
} 

期望的結果是,它會跳過包括。

回答

-1

的包括保持發生。

所以我只是做了包含動態。

---- defaults/main.yml 
mode: "create" 

---- tasks/main.yml 
- include: "{{ mode + '.yml' }}" 
1

當在hostvars下定義變量的值時,不需要使用boolJinja filter

將值轉換爲某些類型,例如當您從vars_prompt輸入字符串爲「True」並且系統不知道它是布爾值時。

因此,一個簡單

when: not destroy 

應的伎倆。

9

運行任務時destroytrue

--- 
- hosts: localhost 
    connection: local 
    vars: 
    destroy: true 
    tasks: 
    - debug: 
     when: destroy 

destroyfalse:在當前

--- 
- hosts: localhost 
    connection: local 
    vars: 
    destroy: false 
    tasks: 
    - debug: 
     when: not destroy