How to source a file which contains environment variables in a linux

Hi All,

  • name: “source then env file”
    shell: . /home/myfiles/setup.env

I used . & source to source the env file, but its was not happening the way that works in linux environement

my setup.env:
export ip_net=xx.xx.xx
export home_net=xx.xx.xx

expected:

gopi@xx.xx.xx.xx> . /home/myfiles/setup.env
gopi@xx.xx.xx.xx> echo $ip_net

xx.xx.xx.xx

gopi@xx.xx.xx.xx>

Actual:
gopi@xx.xx.xx.xx> echo $ip_net

gopi@xx.xx.xx.xx>

its throwing empty for the variable ip_net, how to achieve this

Tasks execute independently, so it DOES source it, but it does not
make it available for the next task.

Look at the 'environment' keyword if you want to set such vars for your tasks.

i want to get this as a linux environment variables, after the playbook has finished it needs to setup some environment variable to linux level.
While a shell script is doing this by simply sourcing the file, why the same shell command is not working under ansible task

Gopi Krishna [22.02.2018 06:18]:

i want to get this as a linux environment variables, after the playbook has
finished it needs to setup some environment variable to linux level.
While a shell script is doing this by simply sourcing the file, why the
same shell command is not working under ansible task

Brian answered your question 8 hours ago.

Gopi Krishna, Did you got any solution for this? Now I was in the same situation.

Gopi Krishna, Did you got any solution for this? Now I was in the same situation.

I can expect that any statements executed within the same shell task will have access to the variables, but nowhere else in the system.

What happens if you do the following?:

- name: "source then env file"
shell: |
source /home/myfiles/setup.env
echo $ip_net
echo $home_net

I'm guessing you should get the values you expect.

If you want to configure shell variables to be available system wide for interactive use, look into modifying .bashrc or .bash_profile

Hugo G.