How to constantly set an environment variable?

Host - Ubuntu 16.04

I am trying to set an environment variable for a user using:

- 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')}} 

Unfortunately, it outputs:

 TASK [debug] ******************************************************************* ok: [xxxxx.xxx] => { "msg": "" } 

How can I export a variable, so the next time I run some tasks on this computer, I can use {{ lookup('env', 'MY_VAR') }} to get the value of this variable?

+5
source share
2 answers

Since search queries occur locally, and since each task runs in its own process, you need to do something a little different.

 - 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 

In this example, I select .bashrc and check var in the same command and save the value with register

+5
source

All searches in Ansible are local. For more details see:

Note Search is performed on the local computer, not on the remote computer.

+1
source

Source: https://habr.com/ru/post/1257397/


All Articles