ansible 运行 .bash_profile 的问题 [英] Issue with running .bash_profile by ansible
问题描述
我想通过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 play 设置?无论如何,.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 play的任务设置,可以在你的剧本中设置:
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 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!