---
- 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).
---
- hosts: all
tasks:
- name: Export environment variable
shell: /usr/local/bin/blackbox
---
- hosts: all
tasks:
- name: Export environment variable
shell: /usr/local/bin/blackbox && env | grep ^ANIMAL
register: output
But the real scenario is that I may have 5-10 environment variables generated from blackbox and I am looking for a structured way to access them.
Thanks again.