Trying to understand retrieving environment variables on a remote server

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?

setup provides you with ansible_env. For example,

    - setup:
        gather_subset: env
    - debug:
        var: ansible_env

gives

    ansible_env:
        BLOCKSIZE: K
        HOME: /home/admin
        LANG: C.UTF-8
        LOGNAME: admin
        MAIL: /var/mail/admin
        MM_CHARSET: UTF-8
        PATH: /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin
        PWD: /home/admin
        SHELL: /bin/sh
        SSH_CLIENT: 10.1.0.27 33152 22
        SSH_CONNECTION: 10.1.0.27 33152 10.1.0.157 22
        TERM: su
        USER: admin

The command sh -c set provides you with the same (with the addition of IFS and PS*)

    - command: sh -c set
      register: out
    - debug:
        var: out.stdout

gives

    out.stdout: |-
        BLOCKSIZE=K
        HOME=/home/admin
        IFS=$' \t
        '
        LANG=C.UTF-8
        LOGNAME=admin
        MAIL=/var/mail/admin
        MM_CHARSET=UTF-8
        OPTIND=1
        PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin
        PPID=53530
        PS1='$ '
        PS2='> '
        PS4='+ '
        PWD=/home/admin
        SHELL=/bin/sh
        SSH_CLIENT='10.1.0.27 33152 22'
        SSH_CONNECTION='10.1.0.27 33152 10.1.0.157 22'
        TERM=su
        USER=admin

Try

    - command: bash -c set
      register: out
    - debug:
        var: out.stdout

You can use the environment keyword if JAVA_HOME isn’t set. For example,

    - command: sh -c 'echo Java is installed at $JAVA_HOME'
      register: out
      environment:
        JAVA_HOME: /opt/tmp
    - debug:
        var: out.stdout

gives

    out.stdout: Java is installed at /opt/tmp
1 Like