由ansible运行.bash_profile的问题

时间:2015-08-28 09:54:21

标签: ansible

我想通过ansible导出多个变量。我在我的剧本中有这项任务

- 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

1 个答案:

答案 0 :(得分:5)

你的目标是什么?您想永久设置这些变量还是仅用于您的ansible游戏?无论如何.bash_profile可能不是最佳解决方案。

Ansible为每个任务启动一个新的ssh连接。如果您在一项任务中提取.bash_profile来设置环境变量,则在下一项任务中它将无法使用。

如果您想永久设置它,可以像/etc/environment模块一样将其写入lineinfile

如果你只想为Ansible游戏的任务设置它,你可以set it in your playbook

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