问题描述
我想通过ansible导出多个变量.我的剧本中有这项任务
I want to export multiple variables through ansible. I have this task in my playbook
- 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,但无法正常工作
How to run .bash_profile?I have written this code to run .bash_profile but it could not work
- name: Run the bash_profile
shell: source /root/.bash_profile
推荐答案
您的目标是什么?您要永久设置这些变量还是仅用于您的ansible游戏?无论如何,.bash_profile
可能不是最佳解决方案.
What's your goal? Do you want to permanently set those vars or only for your ansible play? In any case .bash_profile
probably is not the best solution.
Ansible为每个任务启动一个新的ssh连接.如果要在一个任务中提供.bash_profile
来设置环境变量,那么在下一个任务中它将不可用.
Ansible initiates a new ssh connection for every task. If you'd source your .bash_profile
in one task to set an environment var, in the next task it wouldn't be available.
如果要永久设置它,可以像以前一样使用lineinfile
模块将其写入/etc/environment
.
If you want to set it permanently, you can write it to /etc/environment
, with the lineinfile
module just like you did.
如果您只想将其设置为Ansible剧本的任务,则可以在您的剧本中进行设置:
If you only want to set it for the tasks of the Ansible play, you can set it in your playbook:
- hosts: ...
environment:
JAVA_HOME: /usr/java/jdk{{jdk_version}}
MIRA_ROOT: /opt/data/rg01/mira
tasks: ...
roles: ...
这篇关于通过Ansible运行.bash_profile的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!