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

**URL:** <https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810>\
**Category:** Ansible Project\
**Created:** [February 21, 2018, 6:57am UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810 "2018-02-21T06:57:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gopi\_Krishna](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@Gopi\_Krishna](https://forum.ansible.com/u/Gopi_Krishna)\
**Post date:** [February 21, 2018, 6:57am UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/1 "2018-02-21T06:57:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 21, 2018, 9:07pm UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/2 "2018-02-21T21:07:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gopi\_Krishna](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@Gopi\_Krishna](https://forum.ansible.com/u/Gopi_Krishna)\
**Post date:** [February 22, 2018, 5:18am UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/3 "2018-02-22T05:18:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [February 22, 2018, 7:31am UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/4 "2018-02-22T07:31:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [February 20, 2020, 9:53am UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/5 "2020-02-20T09:53:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 21, 2020, 10:37pm UTC](https://forum.ansible.com/t/how-to-source-a-file-which-contains-environment-variables-in-a-linux/26810/6 "2020-02-21T22:37:23Z")

</div>

> 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.
