2015-08-28 35 views
1

我想通過ansible導出多個變量。我有這樣的任務,我的劇本運行.bash_profile出現問題的原因

- name: Set the MIRA_ROOT and Binding_Address in Profile 
    lineinfile: dest=/root/.bash_profile insertbefore='^export PATH' line=" {{item.line}}" 
    with_items: 
     - { line: 'export JAVA_HOME=/usr/java/jdk{{jdk_version}}' } 
     - { line: 'export MIRA_ROOT=/opt/data/rg01/mira' } 

如何運行.bash_profile中?我寫了這個代碼的.bash_profile運行,但它不能正常工作

- name: Run the bash_profile 
    shell: source /root/.bash_profile 

回答

4

什麼是你的目標是什麼?你想永久設置這些變量還是僅僅爲了你的演奏?無論如何.bash_profile可能不是最好的解決方案。

Ansible爲每個任務啓動一個新的ssh連接。如果你想在一個任務中設置.bash_profile來設置環境變量,那麼在下一個任務中它將不可用。

如果你想永久設置它,你可以把它寫到/etc/environmentlineinfile模塊就像你一樣。

如果你只是想設置它的Ansible戲的任務,你可以set it in your playbook

- hosts: ... 
    environment: 
    JAVA_HOME: /usr/java/jdk{{jdk_version}} 
    MIRA_ROOT: /opt/data/rg01/mira 
    tasks: ... 
    roles: ... 
+0

我要永久設定的變量,我試圖在/ etc /環境,但它不能正常工作請用示例幫助我 – user3345734