4

I have a problem with ansible playbook. I am trying to run a Java jar as a command. Whenever I run this directly on the virtual machine - it works all the time:

java -jar Installer20161018.jar -readImage Linux_x86-64_20161111.zip -installDir /opt/installPath/vf5511/instDir

important information: the installation HAS to be run from user vf5511, and his home folder is /opt/installPath/vf5511

But when trying to write a playbook and run it - it gets all wrong.

This is the playbook:

---
- hosts: webmwc10
  become: yes
  become_user: wm5511
  become_method: sudo
  tasks:
    - name: installing server
      shell: java -jar Installer20161018.jar -readImage Linux_x86-64_20161111.zip -installDir /opt/installPath/vf5511/instDir

When I run the playbook, I get an error:

"rc": 127,
"start": "2017-06-02 09:21:31.931049",
"stderr": "/bin/sh: java: command not found",
"stderr_lines": [
    "/bin/sh: java: command not found"
],
"stdout": "",
"stdout_lines": []

Java not found? I don't understand this. Java is installed and working properly!

Can anyone help me with this?

Zibi Smough
  • 113
  • 1
  • 2
  • 11
  • 1
    The JDK/JRE's `bin` dir probably isn't found in the `$PATH` of that user. One workaround would be to use an absolute path to the `java` executable instead of relying on the `$PATH`, but I don't know what would be the best practice in term of `Ansible` scripts. – Aaron Jun 02 '17 at 09:35
  • how did you install Java ? sounds like JAVA_HOME is not set, you can check https://stackoverflow.com/a/31775587/4296747 to set correctly the env variables – Frederic Henri Jun 02 '17 at 09:42
  • You can also provide the environment variables (such as PATH) in the `environment` keyword on the play: http://docs.ansible.com/ansible/playbooks_environment.html – Jack Jun 02 '17 at 11:46
  • Give the full path to java in your shell task and post back if it doesn't work. – Zlemini Jun 02 '17 at 22:30

3 Answers3

4

Run below commands on your target server to rule out Java issues

    which java
    java -version

Upon successful results add quotes to your shell command like below and run the playbook again.

    shell: "java -jar Installer20161018.jar -readImage Linux_x86-64_20161111.zip -installDir /opt/installPath/vf5511/instDir"
0

You should add your java address before "java". This problem may be occurs when using ssh too. For example:

shell: /your_java_address_in_target_server/java -jar Installer20161018.jar -readImage Linux_x86-64_20161111.zip -installDir /opt/installPath/vf5511/instDir
0

#1. make sure you "become_user" who has access to java #2. In the .bash_profile, make sure you are setting the Java home path. #3. Before calling the java command, run .bash_profile to make sure the JDK path is set. Eg: - name: unjar abc.jar shell: source ~/.bash_profile; jar xvf abc.jar