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:

---

- hosts: all

 tasks:

   - name: Export environment variable

     shell: export ANIMAL=dropbear


   - debug: msg="{{ ansible_env.ANIMAL }}"


Unless 'ANIMAL' already exists in the environment (ie. outside of the first 
shell task), the debug message won't be able to access the value of ANIMAL.


I have investigated several different methods such as:


1. Registering the shell command and printing 'env' to stdout and parsing

2. Dumping to file on filesystem and using wrapper to load/source file from 
disk in subsequent tasks

3. Parsing a file dumped to disk with include_vars / lookup with ini plugin



All methods seem a little too 'hacky' and I was wondering if anyone had a 
more elegant solution that didn't involve storing the information on disk?


I was hoping there was a variable exposed when registering the shell task 
so you can access environment variables eg. 
'registered_var.env_vars.ANIMAL', similar to how you access stdout/stderr 
etc. Is there something this obvious that I am missing?


It appears there has been similar discussion about environment variables 
set in Ansible shell tasks not persisting across tasks here, but it looks 
to end up with a similar conclusion:

http://stackoverflow.com/questions/27733511/how-to-set-linux-environment-variables-with-ansible


All suggestions for making this process more streamlined will be much 
appreciated! (passing values through etcd at the moment).

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/668f42b3-31f8-4fff-bc7e-c5cdc6354835%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to