# Creating environmental variable in ansible playbook

**URL:** <https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986>\
**Category:** Ansible Project\
**Created:** [July 14, 2017, 12:06pm UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986 "2017-07-14T12:06:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prakash\_Khadka](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Prakash\_Khadka](https://forum.ansible.com/u/Prakash_Khadka)\
**Post date:** [July 14, 2017, 12:06pm UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/1 "2017-07-14T12:06:01Z")

</div>

I have created a playbook. I want to define environmental variables when I execute a task.

- name: Executing scripts  
shell: sh /root/something.sh  
register: shell  
environment:  
SOMETHING: something

This is not working. any idea?

---

<div class="post-metadata">

**Author:** ![Olof\_Lundgren](https://avatars.discourse-cdn.com/v4/letter/o/e19b73/32.png) [@Olof\_Lundgren](https://forum.ansible.com/u/Olof_Lundgren)\
**Post date:** [July 14, 2017, 12:41pm UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/2 "2017-07-14T12:41:12Z")

</div>

I did not get that working either.

I ended up putting my environment variables in the actual environment in Linux. Like this:

export ANSIBLE\_NET\_USERNAME=vyos  
export ANSIBLE\_NET\_PASSWORD=password

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 14, 2017, 2:32pm UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/3 "2017-07-14T14:32:45Z")

</div>

Some bugs in you script maybe, this works for me.

$ more something.sh  
#!/bin/sh  
echo $SOMETHING

playbook.yml  
- hosts: localhost  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Executing scripts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: sh something.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: shell  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SOMETHING: Yabba Dabba Doo!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=shell.stdout

Output from ansible-playbook

PLAY [localhost]

---

<div class="post-metadata">

**Author:** ![Prakash\_Khadka](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Prakash\_Khadka](https://forum.ansible.com/u/Prakash_Khadka)\
**Post date:** [July 16, 2017, 7:47am UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/4 "2017-07-16T07:47:27Z")

</div>

my bad. Yes it is working. Anyway to define it globally as long as all task runs? I tried to set it in top of tasks it shows error:

---

<div class="post-metadata">

**Author:** ![Prakash\_Khadka](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Prakash\_Khadka](https://forum.ansible.com/u/Prakash_Khadka)\
**Post date:** [July 16, 2017, 7:48am UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/5 "2017-07-16T07:48:31Z")

</div>

It should work. See example from @Kai Stian Olstad

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 22, 2017, 1:21pm UTC](https://forum.ansible.com/t/creating-environmental-variable-in-ansible-playbook/24986/6 "2017-07-22T13:21:33Z")

</div>

You can only use environment on play, role, block or task.
