Question about best practice approach to indicate that something is already "setup"

Installing Oracle Java is not straight forward because it is not delivered via Apt or in an RPM.

I have written a role, "java", that does this work and I want to be able to detect that Java is already installed when this role is requested subsequently so that I do not run its steps.

What is the best practice approach to indicate that "Java is already installed"?

I can see putting a marker file somewhere - such as /etc/java-version and the contents would have the version number and then the role steps could have a conditional looking for this marker. However, this approach does not seem very elegant.

Suggestions for a best practices solution for this?

Thank you!

Randy

I would worry that someone will delete /etc/java-version one day and blow everything up.

Could you attempt to actually run something like shell: “java --version” and then register the output to a variable? You could then put a when on the java role that checks the contents of the output, possibly even for a certain version.

hth,
Mark

For an “enterprise” scenario, the preferred approach (imo) is to automate packaging the jre as an rpm|deb or find a third party repo.

https://github.com/ksyz/oracle-java-rpm

Hi Randy,

I had the same case but couldn’t find an RPM version besides the java openjdk which is not suitable for our application, this is the tasks I’m using in Ansible to install Java jdk-1.6.0_26-fcs.x86_64 version downloaded from Oracle and saved in a S3 bucket :

“”"

  • name: register if java version jdk-1.6.0_26-fcs.x86_64 is installed
    shell: rpm -qa | grep jdk
    register: java_version
    ignore_errors: yes

  • name: downloading java binary from s3
    s3: bucket=mybucket object=packages/jdk-{{ JDK_VERSION }}-linux-x64-rpm.bin dest={{ DOWNLOAD_DIR }}/jdk-{{ JDK_VERSION }}-linux-x64-rpm.bin mode=get aws_access_key={{ AWS_ACCESS_KEY_ID }} aws_secret_key={{ AWS_SECRET_ACCESS_KEY }}
    when: java_version.stdout != “jdk-1.6.0_26-fcs.x86_64”

  • name: making java binary executable
    file: dest={{ DOWNLOAD_DIR }}/jdk-{{ JDK_VERSION }}-linux-x64-rpm.bin mode=0544
    when: java_version.stdout != ‘jdk-1.6.0_26-fcs.x86_64’

  • name: Installing Java
    shell: yes | /bin/sh {{ DOWNLOAD_DIR }}/jdk-{{ JDK_VERSION }}-linux-x64-rpm.bin
    when: java_version.stdout != ‘jdk-1.6.0_26-fcs.x86_64’
    “”"

Regards,
Nicolas.

Thank you for the replies.

After some experimentation with the proper way to test for values using the “when:” construct, I came up with the following:

  • name: Determine which version of Java is installed
    shell: java -version 2>&1 | grep version | cut -d ’ ’ -f 3
    register: java_version
    ignore_errors: yes

  • name: Copy Java 7u45 to compute instance
    copy:
    src=jdk-7u45-linux-x64.tar.gz
    dest=/home/{{ user }}/jdk-7u45-linux-x64.tar.gz
    owner={{ user }}
    group={{ user }}
    mode=600
    when: java_version.stdout != ‘“1.7.0_45”’

– Randy

Nicolas G wrote: