4

I have an Ansible playbook which expects to find JAVA_HOME in ansible_env. When I run it against an Ubuntu 16.04 host with Oracle Java 8 installed remotely, JAVA_HOME and the other Java variables are missing. However, when I collect the facts locally on the target host using ansible -K --become -m setup -c local localhost I see them. Both my management workstation and the target host are Ubuntu 16.04 running Ansible 2.5 installed via APT. What's causing hte difference? How can I make the JAVA_HOME visible to Ansible when it's running remotely?

Note that the question is not about how to set JAVA_HOME for the duration of a playbook's execution, nor about a workaround that invokes a shell. I seek to understand why JAVA_HOME isn't present where one would expect to find it to begin with.

Environment gathered remotely:

"ansible_env": {
    "HOME": "/root", 
    "LANG": "en_US.UTF-8", 
    "LANGUAGE": "en_US:en", 
    "LC_ADDRESS": "he_IL.UTF-8", 
    "LC_ALL": "en_US.UTF-8", 
    "LC_IDENTIFICATION": "he_IL.UTF-8", 
    "LC_MEASUREMENT": "he_IL.UTF-8", 
    "LC_MESSAGES": "en_US.UTF-8", 
    "LC_MONETARY": "he_IL.UTF-8", 
    "LC_NAME": "he_IL.UTF-8", 
    "LC_NUMERIC": "en_US.UTF-8", 
    "LC_PAPER": "he_IL.UTF-8", 
    "LC_TELEPHONE": "he_IL.UTF-8", 
    "LC_TIME": "en_US.UTF-8", 
    "LOGNAME": "root", 
    "MAIL": "/var/mail/root", 
    "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin", 
    "PWD": "/home/local/VELOQUANT/yitzikc", 
    "SHELL": "/bin/bash", 
    "SUDO_COMMAND": "/bin/sh -c echo BECOME-SUCCESS-ejewjujifuuqrzzrkbjlmewadhlixwgh; /usr/bin/python /home/local/VELOQUANT/yitzikc/.ansible/tmp/ansible-tmp-1524498140.22-96857206506570/setup.py", 
    "SUDO_GID": "527958529", 
    "SUDO_UID": "527959121", 
    "SUDO_USER": "yitzikc", 
    "TERM": "xterm", 
    "USER": "root", 
    "USERNAME": "root"
}

Environment gathered locally:

    "ansible_env": {
        "DERBY_HOME": "/usr/lib/jvm/java-8-oracle/db", 
        "DISPLAY": "localhost:10.0", 
        "GTK_MODULES": "gail:atk-bridge", 
        "HOME": "/root", 
        "J2REDIR": "/usr/lib/jvm/java-8-oracle/jre", 
        "J2SDKDIR": "/usr/lib/jvm/java-8-oracle", 
        "JAVA_HOME": "/usr/lib/jvm/java-8-oracle", 
        "LANG": "en_US.UTF-8", 
        "LANGUAGE": "en_US:en", 
        "LC_ADDRESS": "he_IL.UTF-8", 
        "LC_ALL": "en_US.UTF-8", 
        "LC_IDENTIFICATION": "he_IL.UTF-8", 
        "LC_MEASUREMENT": "he_IL.UTF-8", 
        "LC_MESSAGES": "en_US.UTF-8", 
        "LC_MONETARY": "he_IL.UTF-8", 
        "LC_NAME": "he_IL.UTF-8", 
        "LC_NUMERIC": "en_US.UTF-8", 
        "LC_PAPER": "he_IL.UTF-8", 
        "LC_TELEPHONE": "he_IL.UTF-8", 
        "LC_TIME": "en_US.UTF-8", 
        "LESSCLOSE": "/usr/bin/lesspipe %s %s", 
        "LESSOPEN": "| /usr/bin/lesspipe %s", 
        "LOGNAME": "root", 
        "MAIL": "/var/mail/yitzikc", 
        "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin", 
        "PWD": "/home/local/VELOQUANT/yitzikc", 
        "SHELL": "/bin/bash", 
        "SHLVL": "1", 
        "SSH_CLIENT": "172.30.1.183 51808 22", 
        "SSH_CONNECTION": "172.30.1.183 51808 172.30.1.121 22", 
        "SSH_TTY": "/dev/pts/0", 
        "SUDO_COMMAND": "/bin/sh -c echo BECOME-SUCCESS-injqonohmutwfdyspntuiuaanynmjljw; /usr/bin/python /home/local/VELOQUANT/yitzikc/.ansible/tmp/ansible-tmp-1524498257.19-88347143844147/setup.py", 
        "SUDO_USER": "yitzikc", 
        "TERM": "xterm", 
        "USER": "root", 
        "USERNAME": "root", 
        "XDG_DATA_DIRS": "/usr/local/share:/usr/share:/var/lib/snapd/desktop", 
        "XDG_RUNTIME_DIR": "/run/user/527959121", 
        "XDG_SESSION_ID": "73", 
        "_": "/usr/bin/ansible"
    }, 
Isac Casapu
  • 1,163
  • 13
  • 21
  • Could you please point me to the question this presumably duplicates? I couldn't find it... Anyway, the answer for my problem was to eschew the use of JAVA_HOME altoghther, instead using the solution proposed here: https://askubuntu.com/a/657468/488782 Please re-open so I could post a fuller answer. – Isac Casapu Apr 24 '18 at 09:06
  • Adding to my comment above, the first answer is about setting environment variables for the duration of a playbook's execution, which is irrelevant for me in this case, and the second uses a workaround and doesn't address the questions of why JAVA_HOME isn't present where one would expect to find it to begin with. – Isac Casapu Apr 24 '18 at 09:24
  • Please note that the second answer clearly explains you that there are login and non-login shell sessions and not all files are sourced in all cases. Also please note that the question you ask: "*How do I need to set an environment variable on a *nix system, so that it is sourced for either login and non-login shell session*" is **not a programming question** hence it is off-topic for StackOverflow. – techraf Apr 28 '18 at 04:59
  • My question was never about setting an environment variable, but about an apparent inconsistency in the environment presented by Ansible. The behavior I witnessed was very surprising and unintuititive, and I believe it's a legitmate question that can save time to other Ansible automation developers. I did accept that answer since it pointed to the underlying issue. I'd like to post a complementary answer that show my actual solution, which eschews the use of ansible_env altogether, but now I can no longer add answers. – Isac Casapu Apr 29 '18 at 09:55
  • It has **nothing** to do with Ansible. Run `ssh -c` against your target server and you will get the same result. – techraf Apr 29 '18 at 10:58

1 Answers1

1

Doing a test on my fedora, if you put the JAVA_HOME to .bash_profile, Ansible will not pick it up. But if you put it in .bashrc file, it will show up to ansible_env.

PS. When you initiate gather facts to localhost it will be inheriting all the env variables you have from your shell. if you have for example executed: export MY_VAR=YitzikC prior to running your playbook towards localhost, you will see the variable in the ansible_env.

ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • 1
    I see your point, running instead: /usr/bin/env -i ansible -K --become -m setup -c local localhost yields a much more minimalistic environment that lacking JAVA_HOME. – Isac Casapu Apr 24 '18 at 08:55
  • 1
    i believe the difference in your case is what the guy from the linked thread worded better: that `Ansible logins via SSH using non-login shell.`, meaning the .bash_profile is not sourced, while the .bashrc is. when you were gathering facts from localhost, the JAVA_HOME was there because you were already in a login shell, so it was inherited. But when you executed to remote host, the .bash_profile was not sourced due to the non-login shell explained previously. at least this is my understanding. – ilias-sp Apr 24 '18 at 10:40