# Setting Target Machine Environment Variables

**URL:** <https://forum.ansible.com/t/setting-target-machine-environment-variables/22872>\
**Category:** Ansible Project\
**Created:** [October 25, 2016, 12:11am UTC](https://forum.ansible.com/t/setting-target-machine-environment-variables/22872 "2016-10-25T00:11:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hgebrekidan](https://avatars.discourse-cdn.com/v4/letter/h/848f3c/32.png) [@hgebrekidan](https://forum.ansible.com/u/hgebrekidan)\
**Post date:** [October 25, 2016, 12:11am UTC](https://forum.ansible.com/t/setting-target-machine-environment-variables/22872/1 "2016-10-25T00:11:14Z")

</div>

Hello, Ansible newbie here. I seem to be having a hard time understanding how to set target machine environmental variables, even after trying the several example solutions that I’ve found around the web.

Here are a few techniques that I’ve tried:

\*As a separate play

- name: “try this env”  
shell: export APP\_HTTP\_PORT=8081

\*As part of the application kick-up command:

- name: “Start example Node.js app.”  
command: sudo forever -l ~/log.log -e ~/err.log -o /out.log /var/www/appfolder/index.js  
environment:  
APP\_HTTP\_PORT: “8001”  
…

\*As part of a host definition (variable)

- hosts: ec2  
name: configuration play  
user: ec2-user  
become: yes  
gather\_facts: true

environment: “{{ app\_environment }}”

\*As part of a host definition (direct)

- hosts: ec2  
name: configuration play  
user: ec2-user  
become: yes  
gather\_facts: true

environment:  
APP\_HTTP\_PORT: “8001”  
…

In each case, my application evaluates for the presence of specific env variables, and based on the logs I have, the variables are not available to the application. When using debug mode (-vvvv) I do indeed see the env variables in my console, and I see the variable being output with an expected value when I issue an “echo” command from the playbook - but not when I ssh into the server and run the identical commands. The app’s logs indicate missing variables as well. I hope someone here would be able to point me in the right direction - 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:** [March 3, 2017, 10:04pm UTC](https://forum.ansible.com/t/setting-target-machine-environment-variables/22872/2 "2017-03-03T22:04:05Z")

</div>

each task is a different session, env vars created in task1, will not  
be seen in task2 (if you login from one terminal and expot a variable,  
you won't see it when you login from a 2nd terminal).

Use the environment: directive at play/block/task level if you want  
your tasks to have a specific set environment. for example:  
[http://docs.ansible.com/ansible/playbooks\_environment.html](http://docs.ansible.com/ansible/playbooks_environment.html)
