远程收集事实时,JAVA_HOME不在Ansible环境中

时间:2018-04-23 16:06:19

标签: java ansible environment-variables ubuntu-16.04

我有一个Ansible playbook,期望在ansible_env中找到JAVA_HOME。当我在远程安装Oracle Java 8的Ubuntu 16.04主机上运行它时,JAVA_HOME和其他Java变量都丢失了。但是,当我使用 ansible -K --become -m setup -c local localhost 在目标主机上本地收集事实时,我看到了它们。我的管理工作站和目标主机都是通过APT安装运行Ansible 2.5的Ubuntu 16.04。导致差异的是什么?当Ansible远程运行时,如何让JAVA_HOME可见?

请注意,问题不是关于如何在剧本执行期间设置JAVA_HOME,也不是关于调用shell的变通方法。我试图理解为什么JAVA_HOME并不存在于人们期望从一开始就找到它的地方。

远程收集环境:

"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"
}

本地聚集的环境:

    "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"
    }, 

1 个答案:

答案 0 :(得分:1)

对我的fedora进行测试,如果你将JAVA_HOME放到.bash_profile,Ansible将不会接受它。但是如果你把它放在.bashrc文件中,它将显示为ansible_env

PS。当您开始收集事实到localhost时,它将继承您从shell获得的所有env变量。如果您在将playbook运行到localhost之前执行了export MY_VAR=YitzikC,则会在ansible_env中看到该变量。