2016-06-21 43 views

回答

2

這裏是一個回答你的問題,我認爲你不能使用env作爲變量,因爲它在ansible保留字:

--- 
- hosts: all 
    gather_facts: no 
    vars: 
    ENV: "dev" 
    tasks: 
    - debug: 
     msg: "{%- if ENV == 'dev' -%} tom {%- else -%} mike {%- endif -%}" 

結果時ENVdev

% ansible-playbook -i "localhost," test.yml -c local 

PLAY [all] ********************************************************************* 

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "msg": "tom" 
} 

PLAY RECAP ********************************************************************* 
localhost     : ok=1 changed=0 unreachable=0 failed=0 

ENV的值是其他值時的結果:

% ansible-playbook -i "localhost," test.yml -c local 

PLAY [all] ********************************************************************* 

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "msg": "mike" 
} 

PLAY RECAP ********************************************************************* 
localhost     : ok=1 changed=0 unreachable=0 failed=0 

希望對你有所幫助

1

一種方法是使用另一個查找到包含用戶ENV映射JSON文件。例如: -

user_envs.json:

{ 
    "dev": "tom", 
    "prod": "mike" 
} 

查找:

{{ (lookup('file', 'user_envs.json') | from_json).get('dev') }} 

不具有包膜嘗試一下,但現在給它一個鏡頭!