getting error while checking which java using ansible

57 views
Skip to first unread message

ruben melvin

unread,
Jan 22, 2019, 10:05:06 AM1/22/19
to Ansible Project
Hi guys,

I am trying to execute below ansible playbook.

---
- name: "checking the java version"
  hosts: host
  remote_user: myremoteuser
  gather_facts: False
  become: yes
  become_user: serveruser
  become_method: 'sudo'

  tasks:
    - name: which java
      shell: which java
      args:
        executable: /usr/java/
      register: java
    
  
    - debug: msg="{{ java }}"
...


and getting the below error. 

FAILED! => {"changed": false, "cmd": "/usr/java/ -c 'which java'", "msg": "[Errno 13] Permission denied", "rc": 13} 


this is what I execute.. 


ansible-playbook -i hosts java.yml -vv --ask-become-pass


I will have to login first using "myremoteuser" and then I have to change the user to "serveruser" and execute the above command. 

I am able to do this directly on server but not through ansible. 


Kindly help. 


Thanks in advance,

Ruben

Kai Stian Olstad

unread,
Jan 22, 2019, 3:36:43 PM1/22/19
to ansible...@googlegroups.com
On Tuesday, 22 January 2019 16:05:06 CET ruben melvin wrote:
> Hi guys,
>
> I am trying to execute below ansible playbook.
>
> ---
> - name: "checking the java version"
> hosts: host
> remote_user: myremoteuser
> gather_facts: False
> become: yes
> become_user: serveruser
> become_method: 'sudo'
>
> tasks:
> - name: which java
> shell: which java
> args:
> executable: /usr/java/
> register: java

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


--
Kai Stian Olstad


ruben melvin

unread,
Jan 23, 2019, 8:45:33 AM1/23/19
to Ansible Project
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

Karl Auer

unread,
Jan 23, 2019, 9:39:25 AM1/23/19
to ansible-project
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/527b1756-839f-4b6d-9660-ee6745f7aa71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

ruben melvin

unread,
Jan 23, 2019, 10:12:17 AM1/23/19
to Ansible Project
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

Karl Auer

unread,
Jan 23, 2019, 11:56:32 AM1/23/19
to ansible-project
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.


For more options, visit https://groups.google.com/d/optout.

Sanjay

unread,
Jan 24, 2019, 5:08:51 AM1/24/19
to Ansible Project
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 

---
- name: "checking the java version"
  hosts: host
  remote_user: myremoteuser
  gather_facts: False
  environment:
    PATH: '$PATH:/path/to/bin/for/java'
  become: True
  become_user: serveruser
  become_method: 'sudo'

Also, try to figure out if you can utilise become_flags option if above one doesn't help.

ruben melvin

unread,
Jan 28, 2019, 1:24:30 AM1/28/19
to Ansible Project
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. 

---
- name: "checking the java version"
  hosts: host
  remote_user: c5115061
  gather_facts: False
  become: yes
  become_user: idmspp
  become_method: 'sudo'

  tasks:
    - name: which java
      shell: java -version
#      args:
#        executable: /usr/java/
      register: java
    
  
    - debug: msg="{{ java }}"


FAILED! => {"changed": true, "cmd": "java -version", "delta": "0:00:00.074380", "end": "2019-01-28 06:00:14.656299", "msg": "non-zero return code", "rc": 127, "start": "2019-01-28 06:00:14.581919", "stderr": "/bin/bash: java: command not found", "stderr_lines": ["/bin/bash: java: command not found"], "stdout": "", "stdout_lines": []}


Can anyone help me in this?

Thanks,
Ruben

Karl Auer

unread,
Jan 28, 2019, 3:47:54 AM1/28/19
to ansible-project
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages