Hi guys,
I am trying to execute below ansible playbook.
Hi guys,
I am trying to execute below ansible playbook.
Here you are trying to run the directory /usr/java/
just remove the args and executable and it should work.
I tried removing the args and executable, then while trying to execute again I am getting an error below.
{“changed”: true, “cmd”: “which java”, “delta”: “0:00:00.074712”, “end”: “2019-01-23 13:41:10.748039”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-01-23 13:41:10.673327”, “stderr”: “which: no java in (/usr/bin:/bin:/usr/sbin:/sbin)”, “stderr_lines”: [“which: no java in (/usr/bin:/bin:/usr/sbin:/sbin)”], “stdout”: “”, “stdout_lines”: }
Kindly help.
Ruben
It looks as if it’s working. It is running the command “which java”. The “which” command is unable to find an executable called “java” in the path, so it outputs an appropriate message to stderr and returns a non zero value.
It looks as if you are trying to locate the java executable, but that will only work if the executable is in the shell’s PATH. If you know what to add to the PATH variable, you probably no longer need to use which to find java
If you know roughly where it is, e.g., under /usr somewhere, you could use find to locate it:
find -L /usr -executable -type f -name “java” -print
But to be honest, if you will be running java, you probably need to know exactly which executable you should be running. Otherwise you will have to run a battery of tests to make sure that the executable you find is the right executable (version, architecture etc)
Regards, K.
I tried to use the above command as per you suggestion and again got the permission denied error.
fatal: [ma-idmspp-lapp565]: FAILED! => {“changed”: true, “cmd”: “find -L /usr -executable -type f -name "java" -print”, “delta”: “0:00:01.458827”, “end”: “2019-01-23 15:10:26.161587”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2019-01-23 15:10:24.702760”, “stderr”: “find: ‘/usr/share/polkit-1/rules.d’: Permission denied\nfind: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied\nfind: ‘/usr/tmp/lost+found’: Permission denied\nfind: ‘/usr/tmp/.ansible-c5023304’: Permission denied\nfind: ‘/usr/tmp/yum-root-a6LSso’: Permission denied\nfind: ‘/usr/tmp/systemd-private-8e020ca4b1d1423ea4e25c65b1f20330-ntpd.service-7jH9UJ’: Permission denied\nfind: ‘/usr/local/gcs_compliance’: Permission denied\nfind: ‘/usr/local/epic/cache’: Permission denied\nfind: ‘/usr/local/epic/log’: Permission denied\nfind: ‘/usr/local/lib/asset’: Permission denied”, “stderr_lines”: [“find: ‘/usr/share/polkit-1/rules.d’: Permission denied”, “find: ‘/usr/libexec/initscripts/legacy-actions/auditd’: Permission denied”, “find: ‘/usr/tmp/lost+found’: Permission denied”, “find: ‘/usr/tmp/.ansible-c5023304’: Permission denied”, “find: ‘/usr/tmp/yum-root-a6LSso’: Permission denied”, “find: ‘/usr/tmp/systemd-private-8e020ca4b1d1423ea4e25c65b1f20330-ntpd.service-7jH9UJ’: Permission denied”, “find: ‘/usr/local/gcs_compliance’: Permission denied”, “find: ‘/usr/local/epic/cache’: Permission denied”, “find: ‘/usr/local/epic/log’: Permission denied”, “find: ‘/usr/local/lib/asset’: Permission denied”], “stdout”: “/usr/java/jdk64-1.8.0_151/bin/java\n/usr/java/jdk64-1.8.0_151/jre/bin/java\n/usr/java/jre64-1.8.0_111/bin/java\n/usr/java/jre64-1.8.0_111/jre/bin/java”, “stdout_lines”: [“/usr/java/jdk64-1.8.0_151/bin/java”, “/usr/java/jdk64-1.8.0_151/jre/bin/java”, “/usr/java/jre64-1.8.0_111/bin/java”, “/usr/java/jre64-1.8.0_111/jre/bin/java”]}
Kindly help.
Ruben
The command worked. It found several java executables:
/usr/java/jdk64-1.8.0_151/bin/java
/usr/java/jdk64-1.8.0_151/jre/bin/java
/usr/java/jre64-1.8.0_111/bin/java
/usr/java/jre64-1.8.0_111/jre/bin/java
If you look closely at the output you got from your command, you will see that there is a list of strings called “stdout_lines” that contains the paths to the java executables that the command located. You can extract them as simply as result.stdout_lines[0] or whatever.
There were some directories that the find command could not recurse through, so it reported “permission denied” on those directories.
I’m sorry - I can’t offer a complete solution, only ideas.
The most important idea though, is that you almost certainly should not be searching for an executable you intend to run. You should know where it is.
If your use case requires you to search for java, maybe you can compromise: Set up a list of likely locations, look for it in each of those locations in turn, and if it is not found in one of those locations, abort.
If you really want to persist with searching for it, you could use filters to split up the lines in the stdout result, select only those lines that match “.*java$” - those paths will be paths to executables. You could for example run this command:
find -L /usr -executable -type f -name “java” -print 2>/dev/null | grep “/java$”
That command will discard any errors, and return ONLY lines that refer to an executable called “java”. The command will return zero if there are any java executables and will fail with non-zzero code only of there are no java executables found.
Regards, K.
Either you can set/edit default PATH for java bin in /etc/sudoers (visudo) by editing
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Or you can set environment while executing ansible-playbook
I am actually not supposed to add or set anything on that server as it is a production server. So setting environment is not an option for me.
I just need to know the java version using ansible so that it will help me a lot in reducing the manual works of my teammates.
I tried out using the below playbook with “java -version” instead of “which java” but this time also faced error.
Make a list of possible places that a java executable might be, in order of preference, and work through the list checking each location in turn. Or use the shell command I gave you last week to search an entire directory tree for java executables; checking a list of possible candidates will be a lot faster though. When you find one, use that executable in future commands. If you store the information with the host, you can skip the search next time.
Regards, K.