2016-05-11 41 views
3

在我的劇本,我有這樣的:檢查字符串是平等的,三元運營商Ansible

#More things 
- include: deploy_new.yml 
    vars: 
    service_type: "{{ expose_service == 'true' | ternary('NodePort', 'ClusterIP') }}" 
    when: service_up|failed 

expose_service是真的,我想service_type設置爲「NodePort」和「ClusterIP」,否則。

但是,在所有情況下,service_type設置爲False

我在做什麼錯?

回答

7

解決!

service_type: "{{ 'NodePort' if expose_service == 'true' else 'ClusterIP' }}" 
3

在您的示例中,您正在將三元過濾器應用於'true'字符串。實際上,您將expose_service的值與字符串'NodePort'進行比較,結果總是得到false

您需要封閉相等運算符子句中括號:

- include: deploy_new.yml 
    vars: 
    service_type: "{{ (expose_service == true) | ternary('NodePort', 'ClusterIP') }}" 
    when: service_up|failed 

另外其他兩個點在這個答案解決:

    使用字符串 'true',而不是布爾
  • when指令在錯誤的縮進級別上(您實際上傳遞了名爲01的變量)