# How to set an environment variable on Linux distro for a user using ansible playbook?

**URL:** <https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411>\
**Category:** Ansible Project\
**Tags:** rhel, ubuntu\
**Created:** [October 1, 2015, 9:58pm UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411 "2015-10-01T21:58:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pratik\_Dhandharia](https://avatars.discourse-cdn.com/v4/letter/p/8baadc/32.png) [@Pratik\_Dhandharia](https://forum.ansible.com/u/Pratik_Dhandharia)\
**Post date:** [October 1, 2015, 9:58pm UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411/1 "2015-10-01T21:58:46Z")

</div>

Hi All,

I need to set some environment variable for all the user who is executing the ansible script. Currently I am doing something like this in the playbook:

- hosts: all  
task:

- name: get path  
shell: which R  
register: r\_path

- name: create file in /etc/profile.d  
shell: touch /etc/profile.d/rpath.sh  
sudo: yes  
args:  
creates: /etc/profile.d/rpath.sh

- name: create environment variable and paste in file  
shell: echo “export RHOME={{ r\_path.stdout }}” \> /etc/profile.d/rpath.sh

- name: reload profile  
shell: source /etc/profile

When I run this playbook, it works fine on RHEL machine. But it fails on Ubuntu machine. The reason being, on ubuntu source command is not present.

So what I was wondering is, can this be done in a better way? Or is there a better way to reload the profile on ubuntu ?

Also another question: When we have a playbook with multiple task, does ansible execute all the task as if it were a single session? or Each new task is a separate session with the host?

The reason I am asking is because, in my ansible script, once I have set the environment variable, I would like to use that in the next task.

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 1, 2015, 11:18pm UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411/2 "2015-10-01T23:18:30Z")

</div>

source is not a command its a builtin in bash, ansible uses /bin/sh if  
it is not a symlink to bash or bash is set to do strict sh you won't  
have 'source', but you always have . which does the same thing.

that said

shell: source /etc/profile

is a noop, it does nothing as ansible will open a new shell and that  
info won't be available in other tasks.

you can do this to set an environment var for all tasks in a play:

- hosts: all  
&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RHOME: /path/to/r  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ....

---

<div class="post-metadata">

**Author:** ![Pratik\_Dhandharia](https://avatars.discourse-cdn.com/v4/letter/p/8baadc/32.png) [@Pratik\_Dhandharia](https://forum.ansible.com/u/Pratik_Dhandharia)\
**Post date:** [October 2, 2015, 2:52pm UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411/3 "2015-10-02T14:52:51Z")

</div>

Thanks for clarifying my understanding and suggesting a workaround.

It worked!

---

<div class="post-metadata">

**Author:** ![Ashish\_Srivastava](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Ashish\_Srivastava](https://forum.ansible.com/u/Ashish_Srivastava)\
**Post date:** [May 5, 2018, 6:50am UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-on-linux-distro-for-a-user-using-ansible-playbook/19411/4 "2018-05-05T06:50:28Z")

</div>

Hi All,

I’ve a similar problem, but I want to load environment variable “ORACLE\_HOME” based on the hostname since ORACLE\_HOME is different for each server.  
And, there are n number of hosts defined in inventory, which in turn assigned to groups like dev, sit, uat etc.

In my scenario, I’m passing env\_name to playbook, which resolves hostnames and does the deployment on those servers. I don’t want to keep separate playbook for each server due to different ORACLE\_HOME.

I tried multiple ways to do it. But no luck.

Anyone aware, how it can be achieved?

Thanks,  
Ashish Kumar
