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

52 views
Skip to first unread message

Gopi Krishna

unread,
Feb 21, 2018, 1:57:16 AM2/21/18
to Ansible Project
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:
go...@xx.xx.xx.xx> . /home/myfiles/setup.env
go...@xx.xx.xx.xx> echo $ip_net
xx.xx.xx.xx
go...@xx.xx.xx.xx>


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

go...@xx.xx.xx.xx>

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

Brian Coca

unread,
Feb 21, 2018, 4:08:02 PM2/21/18
to Ansible Project
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.




--
----------
Brian Coca

Gopi Krishna

unread,
Feb 22, 2018, 12:18:43 AM2/22/18
to Ansible Project
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

Werner Flamme

unread,
Feb 22, 2018, 2:32:07 AM2/22/18
to ansible...@googlegroups.com
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.


>
> On Wednesday, February 21, 2018 at 12:27:16 PM UTC+5:30, Gopi Krishna wrote:
>>
>> 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:
>> go...@xx.xx.xx.xx> . /home/myfiles/setup.env
>> go...@xx.xx.xx.xx> echo $ip_net
>> xx.xx.xx.xx
>> go...@xx.xx.xx.xx>
>>
>>
>> Actual:
>> go...@xx.xx.xx.xx> echo $ip_net
>>
>> go...@xx.xx.xx.xx>
>>
>> its throwing empty for the variable ip_net, how to achieve this
>>
>


--


Sivaraman P

unread,
Feb 20, 2020, 4:53:17 AM2/20/20
to Ansible Project
Gopi Krishna, Did you got any solution for this? Now I was in the same situation. 

Hugo Gonzalez

unread,
Feb 21, 2020, 5:37:47 PM2/21/20
to ansible...@googlegroups.com

On 2/20/20 3:53 AM, Sivaraman P wrote:
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.


On Wednesday, 21 February 2018 12:27:16 UTC+5:30, Gopi Krishna wrote:
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:
go...@xx.xx.xx.xx> . /home/myfiles/setup.env
go...@xx.xx.xx.xx> echo $ip_net
xx.xx.xx.xx


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


its throwing empty for the variable ip_net, how to achieve this
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cd73a095-3a89-4f01-b774-352c79d967f3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages