# Accessing environment variables created inside shell task

**URL:** <https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380>\
**Category:** Ansible Project\
**Created:** [January 29, 2016, 9:38am UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380 "2016-01-29T09:38:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PixelDrift.NET\_Sam](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@PixelDrift.NET\_Sam](https://forum.ansible.com/u/PixelDrift.NET_Sam)\
**Post date:** [January 29, 2016, 9:38am UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/1 "2016-01-29T09:38:40Z")

</div>

I am currently working through replacing a legacy orchestration tool with Ansible while maintaining the existing orchestration scripts in place. I have hit an interesting problem with environment variables that I haven’t yet been able to solve. Essentially, I want to be able to access environment variables that are created as part of a shell/comand task. Ideally I would like to extract these variables from the shell task and populate facts with the values and/or pass the values to subsequent tasks. The issue is that I can only access the environment variables in that task (ie. subsequent tasks can’t ‘see’ them) so I need some way of exporting/importing them for other tasks.

The problem is displayed in the following playbook:

---

<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:** [January 29, 2016, 2:56pm UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/2 "2016-01-29T14:56:20Z")

</div>

A few misconceptions here:

#1 what you set in the shell: task is not available to other tasks as  
they each reconnect to the server and open a new ssh session, they  
don't all execute in the same shell process.

#2 ansible\_env is not reset every time you run a task, it reflects the  
environment from when facts where gathered, it can even reflect a  
differnt user when remote\_user/become\_user are set in different  
places.

#3 to set the environment for task execution we have the 'environment'  
directive, which can be set at play, block and task levels:

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ANIMAL: dropbear  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: env |grep ANIMAL  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: output  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=output

---

<div class="post-metadata">

**Author:** ![PixelDrift.NET\_Sam](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@PixelDrift.NET\_Sam](https://forum.ansible.com/u/PixelDrift.NET_Sam)\
**Post date:** [January 29, 2016, 5:20pm UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/3 "2016-01-29T17:20:04Z")

</div>

Brian,

Thanks for the response, I acknowledge all of these points and accept that this is the situation.

In the example at point 3. the environment variable is provided to multiple shell tasks, but in this scenario the value of the environment variable being passed is known prior to execution.

Perhaps I should have better described my constraints, as the example of setting the environment variable in the shell using ‘export’ was only to to show I couldn’t access the value outside the shell.

In my example replace the ‘export ANIMAL=droppear’ to a binary called ‘blackbox’, this ‘blackbox’ binary generates results in the form of setting environment variables that I need to access, one of them named ‘ANIMAL’.

ie.

---

<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:** [January 29, 2016, 5:25pm UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/4 "2016-01-29T17:25:00Z")

</div>

use shell to get the var, register the output, use that output to set  
environment: directive for the rest of the play.

---

<div class="post-metadata">

**Author:** ![PixelDrift.NET\_Sam](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@PixelDrift.NET\_Sam](https://forum.ansible.com/u/PixelDrift.NET_Sam)\
**Post date:** [January 29, 2016, 5:30pm UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/5 "2016-01-29T17:30:13Z")

</div>

To add to that, I understand I can use something similar to the following to access a single value:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [July 11, 2016, 11:51am UTC](https://forum.ansible.com/t/accessing-environment-variables-created-inside-shell-task/20380/6 "2016-07-11T11:51:48Z")

</div>

Custom module for gathering facts for the rescue!  
[http://docs.ansible.com/ansible/developing\_modules.html#module-provided-facts](http://docs.ansible.com/ansible/developing_modules.html#module-provided-facts)  
[http://blog.toast38coza.me/custom-ansible-module-hello-world/](http://blog.toast38coza.me/custom-ansible-module-hello-world/)  
[http://mcsrainbow.github.io/articles/create-an-ansible-module-and-then-use-module-provided-facts.html](http://mcsrainbow.github.io/articles/create-an-ansible-module-and-then-use-module-provided-facts.html)

1. január 29., péntek 18:30:13 UTC+1 időpontban [PixelDrift.NET](http://PixelDrift.NET) Sam a következőt írta:
