# getting error while checking which java using ansible

**URL:** <https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494>\
**Category:** Ansible Project\
**Created:** [January 22, 2019, 3:05pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494 "2019-01-22T15:05:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruben\_melvin](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben\_melvin](https://forum.ansible.com/u/ruben_melvin)\
**Post date:** [January 22, 2019, 3:05pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/1 "2019-01-22T15:05:06Z")

</div>

Hi guys,

I am trying to execute below ansible playbook.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 22, 2019, 8:36pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/2 "2019-01-22T20:36:33Z")

</div>

Here you are trying to run the directory /usr/java/  
just remove the args and executable and it should work.

---

<div class="post-metadata">

**Author:** ![ruben\_melvin](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben\_melvin](https://forum.ansible.com/u/ruben_melvin)\
**Post date:** [January 23, 2019, 1:45pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/3 "2019-01-23T13:45:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 23, 2019, 2:39pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/4 "2019-01-23T14:39:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruben\_melvin](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben\_melvin](https://forum.ansible.com/u/ruben_melvin)\
**Post date:** [January 23, 2019, 3:12pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/5 "2019-01-23T15:12:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 23, 2019, 4:56pm UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/6 "2019-01-23T16:56:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://forum.ansible.com/u/Sanjay1)\
**Post date:** [January 24, 2019, 10:08am UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/7 "2019-01-24T10:08:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ruben\_melvin](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@ruben\_melvin](https://forum.ansible.com/u/ruben_melvin)\
**Post date:** [January 28, 2019, 6:24am UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/8 "2019-01-28T06:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 28, 2019, 8:47am UTC](https://forum.ansible.com/t/getting-error-while-checking-which-java-using-ansible/29494/9 "2019-01-28T08:47:33Z")

</div>

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.
