Ansible - accessing local environment variables
Those variables are in the management machine I suppose source machine in your case.
Check this: https://docs.ansible.com/ansible/devel/collections/ansible/builtin/env_lookup.html
Basically, if you just need to access existing variables, use the ‘env’ lookup plugin. For example, to access the value of the HOME environment variable on management machine:`
Now, if you need to access it in the remote machine you can just run your ansible script locally in the remote machine. Or you could just the ansible facts variables. If it's not in the ansible facts you can just run a shell command to get it.
Use ansible lookup
:
- set_fact: env_var="{{ lookup('env','ENV_VAR') }}"
I have a Linux vm running on osx, and for me:
lookup('env', 'HOME')
returns "/Users/Gonzalo" (the HOME
variable from osx), while ansible_env.HOME
returns "/root" (the HOME
variable from the vm).
Worth to mention, that ansible_env.VAR
fails if the variable does not exists, while lookup('env', 'VAR')
does not fail.