2015-08-21 77 views
3

我需要將通過CLI傳遞給它的具有額外參數的變量替換掉。With Ansible如何使用Jinja2替換變量

./deploy.yml -e 'jira_ticket=REL-78' 

我不能讓它去掉「deploylist/REL-78」,因爲我傳遞「jira_ticket」如果我硬編碼varible(「REL-78」),它完美的作品。

- name: Set fact 
    set_fact: deploy_list"{{ item | replace('deploylist/{{ jira_ticket }}/', '')" 
    with_items: ' {{ modules_to_deploy.value }}' 
    register: deploy_list_result 

ok: [127.0.0.1] => (item=deploylist/REL-78/api) 
ok: [127.0.0.1] => (item=deploylist/REL-78/ariaapi) 
ok: [127.0.0.1] => (item=deploylist/REL-78/ariaquery) 
ok: [127.0.0.1] => (item=deploylist/REL-78/ariaserver) 
ok: [127.0.0.1] => (item=deploylist/REL-78/dashboardidp) 
ok: [127.0.0.1] => (item=deploylist/REL-78/oracle) 
ok: [127.0.0.1] => (item=deploylist/REL-78/uisp) 
ok: [127.0.0.1] => (item=deploylist/REL-78/ui) 

回答

4

這個怎麼樣...(我還添加了=和關閉}}

- name: Set fact 
    set_fact: deploy_list="{{ item | replace('deploylist/'+jira_ticket+'/', '') }}" 
    with_items: modules_to_deploy.value 
    register: deploy_list_result 

注意,您不必字符串化的參數爲with_items

+0

謝謝!我發現一條古老的線索暗示了同樣的事情。花了我一陣子! – luckytaxi