# environment variables for a role

**URL:** <https://forum.ansible.com/t/environment-variables-for-a-role/18627>\
**Category:** Ansible Project\
**Created:** [June 26, 2015, 10:07pm UTC](https://forum.ansible.com/t/environment-variables-for-a-role/18627 "2015-06-26T22:07:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Majid\_al-Dosari](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Majid\_al-Dosari](https://forum.ansible.com/u/Majid_al-Dosari)\
**Post date:** [June 26, 2015, 10:07pm UTC](https://forum.ansible.com/t/environment-variables-for-a-role/18627/1 "2015-06-26T22:07:48Z")

</div>

[http://docs.ansible.com/faq.html#how-can-i-set-the-path-or-any-other-environment-variable-for-a-task-or-entire-playbook](http://docs.ansible.com/faq.html#how-can-i-set-the-path-or-any-other-environment-variable-for-a-task-or-entire-playbook)

i get this. ^^ …but how do i set environment variables for a role? i want it associated with a role and not a playbook.

comment: it seems environment variables are not first-class variables. i just want to use (regular) variables and apply them as environment variables whenever needed (as env vars).

---

<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:** [June 26, 2015, 11:04pm UTC](https://forum.ansible.com/t/environment-variables-for-a-role/18627/2 "2015-06-26T23:04:19Z")

</div>

I'm not sure what you are asking, so I'll respond to the 2  
interpretations I have.

1) add environment variables to all tasks in a role: use environment:  
directive in the role definition, it takes a dictionary from which it  
converts keys into environment variables and assigns the their  
dictionary values to them.

2) you wan to access existing environment variables, you can do so  
through lookup('env', 'varname')

---

<div class="post-metadata">

**Author:** ![Majid\_al-Dosari](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Majid\_al-Dosari](https://forum.ansible.com/u/Majid_al-Dosari)\
**Post date:** [June 26, 2015, 11:30pm UTC](https://forum.ansible.com/t/environment-variables-for-a-role/18627/3 "2015-06-26T23:30:55Z")

</div>

i tried that already. `environment` does not work in role definition. I’m on 1.9.2.

`

- hosts:
- localhost  
gather\_facts: false  
environment:

# ENVVAR: WORKS

roles:

- role: myrole  
environment:  
ENVVAR: DOESNOTWORK

`

---

<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:** [June 30, 2015, 4:28am UTC](https://forum.ansible.com/t/environment-variables-for-a-role/18627/4 "2015-06-30T04:28:21Z")

</div>

sorry, you are correct, environment will added to roles in 2.0 (should  
work in current devel)
