# Is there a way to set ENV variable on target hosts?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950>\
**Category:** Ansible Project\
**Created:** [November 21, 2013, 5:15am UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950 "2013-11-21T05:15:59Z")\
**Posts on this page:** 10\
**Page:** 2

<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:** [April 13, 2015, 1:47pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/21 "2015-04-13T13:47:51Z")

</div>

you have the environment: keyword that can set path per task, in 1.9  
you can do it per play.

---

<div class="post-metadata">

**Author:** ![Eugene\_Sajine](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Eugene\_Sajine](https://forum.ansible.com/u/Eugene_Sajine)\
**Post date:** [April 16, 2015, 8:17pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/22 "2015-04-16T20:17:24Z")

</div>

I’m having problems with some hosts running ansible: It complains that there is no simplejson module found. I figured that it is something to do with the way ansible\_env.PATH is set as somehow it picks up a very old version of python and i can’t figure out where it comes from. I posted the question on stackoverflow and found this thread and other people asking the same thing - but there is still no definitive answer.

I would like to know how this variable is getting set and how i can affect it to make sure that the right version of python is used on the host?  
[http://stackoverflow.com/questions/29289472/ansible-how-ansible-env-path-is-set-in-ssh-session](http://stackoverflow.com/questions/29289472/ansible-how-ansible-env-path-is-set-in-ssh-session)  
[http://grokbase.com/t/gg/ansible-project/1479n0d0qp/ansible-env-path-how-is-it-set](http://grokbase.com/t/gg/ansible-project/1479n0d0qp/ansible-env-path-how-is-it-set)

thanks a lot!

---

<div class="post-metadata">

**Author:** ![Steven\_Haddox](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@Steven\_Haddox](https://forum.ansible.com/u/Steven_Haddox)\
**Post date:** [April 17, 2015, 1:07pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/23 "2015-04-17T13:07:14Z")

</div>

It’s been my experience that ansible absolutely requires Python 2.6+ or Python 2.4 with the simplejson module. I don’t know where in the documents, but I reference this in my README for my main ansible project:

[https://github.com/stevenhaddox/ansible\_rails\_enterprise/blob/master/README.md](https://github.com/stevenhaddox/ansible_rails_enterprise/blob/master/README.md)

I also handle it in my init.yml & as my first step for any server:

[https://github.com/stevenhaddox/ansible\_rails\_enterprise/blob/master/provisioning/init.yml](https://github.com/stevenhaddox/ansible_rails_enterprise/blob/master/provisioning/init.yml)

```auto
# Spin-up the VM and bootstrap the server to add python-simplejson & needed packages

```

```auto
$ vagrant up --no-provision && ansible-playbook provisioning/init.yml -i provisioning/hosts/vagrant -u vagrant -s

```

---

<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:** [April 17, 2015, 2:23pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/24 "2015-04-17T14:23:43Z")

</div>

ansible\_env.PATH is a reflection of the PATH variable on the system  
when ansible gathered facts, ansible does not set the path in a  
special maner. The PATH is set by the host as it would for any other  
non login process (not sourcing .bashrc or other shell equivalents).

If you are having issues using the correct python try setting  
ansible\_python\_interpreter for that host to the correct python.

---

<div class="post-metadata">

**Author:** ![Eugene\_Sajine](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Eugene\_Sajine](https://forum.ansible.com/u/Eugene_Sajine)\
**Post date:** [April 17, 2015, 2:57pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/25 "2015-04-17T14:57:22Z")

</div>

yes, i have misstated the problem in my initial question a bit, sorry.  
I know about the ansible\_python\_interpreter and it does take care of the simplejson error, as instead of using the interpreter that is available in PATH it uses what i have specified.  
So the only question about that variable right now for me is if this can be set globally? I’ll take a look at the ansible.cfg related docs.

as for the PATH or ansible\_env.PATH my current understanding is that the idea is to avoid creation of playbooks that might depend too heavily on t the PATH and therefore may become error prone. The way to change though is through “environment: PATH”.  
Am I correct?

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:** [April 17, 2015, 2:59pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/26 "2015-04-17T14:59:03Z")

</div>

yes, environment is now settable at task/play level, I have it on my  
list to add either a global (ansible.cfg) or inventory setting

---

<div class="post-metadata">

**Author:** ![Vu\_Do](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@Vu\_Do](https://forum.ansible.com/u/Vu_Do)\
**Post date:** [April 23, 2015, 8:18am UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/27 "2015-04-23T08:18:59Z")

</div>

Hi Brian Coca  
Set PATH variable like this in play level work well for me,

environment:  
PATH: “{{ ansible\_env.PATH }}:/usr/local/bin”

I just wonder how you set this in global or inventory setting? Can you show me an example of inventory setting?

---

<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:** [April 23, 2015, 11:29am UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/28 "2015-04-23T11:29:28Z")

</div>

its still on my 'todo' list, cannot show examples until it transitions  
to my 'done' list

---

<div class="post-metadata">

**Author:** ![Osama\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/o/45deac/32.png) [@Osama\_Shaikh](https://forum.ansible.com/u/Osama_Shaikh)\
**Post date:** [March 31, 2016, 10:16pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/29 "2016-03-31T22:16:32Z")

</div>

Hi Brian,

Has there been any update on setting ansible\_python\_interpreter in ansible.cfg.

Our use case is

Inside a system user account, we have our own python 2.7.6 version and we want to use ansible\_python\_interpreter to set our own python path not the one comes by default.

Regards,  
Osama -

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [April 5, 2016, 2:06pm UTC](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950/30 "2016-04-05T14:06:30Z")

</div>

You can set the python interpreter as a host or group var. Normally I set  
`ansible_python_interpreter: /usr/bin/env python`

But in your case it sounds like you’d want a specific path.

[Previous page](https://forum.ansible.com/t/is-there-a-way-to-set-env-variable-on-target-hosts/13950.md?page=1)
