I am new to ansible an I am running through some tutorials. I got stuck with trying to enumerate a specific variable on a remote host. On a "remote host: (RH9.6 in Virtualbox) I set an environment variable, i,e, export JAVA_HOME=/opt/tmp . In my yaml file on the control node I have ‘gather_facts: yes’. Below is the code:
name: variable example
hosts: 192.168.86.43
gather_facts: yes
tasks:
name: show variable values
By defaault ‘lookup’ enumerates env variables from locaal prcessor machine
You need to gather facts on remote machine and extract from there, use ansible_env
task variables have precedence over play variables
debug:
msg: “Java is installed at: {{ absible_env.JAVA_HOME }}”
When I run this I get ‘the task includes an option with an undefined variable…ansible_env’ .
What am I missing? If I enumerate all variables it works, sor of. If I enumerate all variables it does not include the variable I set with the export command. If I go to the remote machine and type ‘env’ and I see the variable i set. If I choose to enumerate ‘ansible_env.HOSTNAME’ which I KNOW is there I get the same error. Spent way too much time on this.
Help! Ty. Ignore the text in the middle of this post , they were comments
How are you exporting the variables you want ansible to have access to? A command like export JAVA_HOME=/opt/tmp is only valid for the user session that created it. For it to be accessible to ansible, it would need to be set in for that login shell.
Not sure i follow “ how are you exporting…”. I am not . I gave export as an example of how I set the variable on the target machine. I “kind of” understand the problem might be context. There is a user called ‘Alan’ on the target machine and logged in as that user I ran the export command to set a variable. I then tried running the Ansible playbook as described to try and enumerate that variable and it fails. How would I export all variables then via Ansible?