如何永久设置环境变量?
问题描述:
主机是Ubuntu的16.04如何永久设置环境变量?
我正在尝试设置环境变量的用户,有:
- hosts: all
remote_user: user1
tasks:
- name: Adding the path in the bashrc files
lineinfile: dest=/home/user1/.bashrc line='export MY_VAR=TEST' insertafter='EOF' state=present
- name: Source the bashrc file
shell: . /home/user1/.bashrc
- debug: msg={{lookup('env','MY_VAR')}}
不幸的是,输出:
TASK [debug] *******************************************************************
ok: [xxxxx.xxx] => {
"msg": ""
}
我怎样才能变量,以便下一次我出口在这台机器上运行一些任务我可以使用{{ lookup('env', 'MY_VAR') }}
来获取这个变量的值?
答
由于查找在本地发生,并且由于每个任务运行在它自己的进程中,所以您需要做一些有点不同的事情。
- hosts: all
remote_user: user1
tasks:
- name: Adding the path in the bashrc files
lineinfile: dest=/home/user1/.bashrc line='export MY_VAR=TEST' insertafter='EOF' state=present
- shell: . /home/user1/.bashrc && echo $MY_VAR
args:
executable: /bin/bash
register: myvar
- debug: var=myvar.stdout
在这个例子中我源发的.bashrc和检查在同一命令中的var和与register