2017-01-19 30 views
0

執行兩個或多個操作嘗試包括名爲Agent-install.yml的文件 並基於主機名稱的前2個字符(例如:dserver或qserver或userver或PSERVER) 要使用SED更新agent.properties.j2到適當值...在Ansible中包含一個文件,並根據條件

也就是說,如果主機名開始與DS/QS /我們再agent.setup.IP = DevMaster1 否則如果主機名從ps開始,然後agent.setup.IP = ProdMaster1

無論如何,他們都應該包括代理安裝.yml (順便說一句,我傳遞的主機名在劇本運行期間,它確實工作 (只是不與2行動)sed部分是我想在這裏添加

任何人有想法如何做到這一點? 我試過有兩個動作使用時的條件(但不認爲這是允許的) 也試過 - 塊允許多個動作,但不允許包括

有沒有更好的方式來做到這一點?

- name: Include if Pre-PROD 
    include: Agent-install.yml 
    local_action: shell sed -i '[email protected]*agent.setup.IP=localhost.*@[email protected]' ../templates/agent.properties.j2 
    when: hosts[0:2] == "ds" or "qs" or "us" 
    ignore_errors: yes 

- name: Include if PROD 
    include: Agent-install.yml 
    local_action: shell sed -i '[email protected]*agent.setup.IP=localhost.*@[email protected]' ../templates/agent.properties.j2 
    when: hosts[0:2] == "ps" 
    ignore_errors: yes 

回答

1

好像你是在錯誤的道路上......

模板用於產生不同的結果文件,根據輸入數據。
您不應該在本地模板sed

修改您agent.properties.j2模板有:

agent.setup.IP={{ agent_ip }} 

然後在你的劇本:

- set_fact: 
    agent_ip: "{{ 'ProdMaster1' if hosts[0:2] == 'ps' else 'DevMaster1' }}" 

- template: 
    src: agent.properties.j2 
    dest: agent.properties.conf 

這在目標主機的方式agent.properties.conf將有適當的值。

+0

謝謝 set_fact獲取此錯誤 FAILED! => {「failed」:true,「msg」:「模板錯誤,模板化字符串:意外字符u \」'\「at 44.字符串: –

+0

DevMaster1在單引號中有一個錯字 –

+0

完美工作,這是絕對是更好的解決方案 –