Skip to content

Instantly share code, notes, and snippets.

@andershedstrom
Created June 16, 2014 11:21
Show Gist options
  • Save andershedstrom/7c7d0bb5b9450c54a907 to your computer and use it in GitHub Desktop.
Save andershedstrom/7c7d0bb5b9450c54a907 to your computer and use it in GitHub Desktop.
Ansible playbook for installing Oracle Java 8 on CentOS
---
- hosts: app
remote_user: vagrant
sudo: yes
vars:
download_url: http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz
download_folder: /opt
java_name: "{{download_folder}}/jdk1.8.0_05"
java_archive: "{{download_folder}}/jdk-8u5-linux-x64.tar.gz"
tasks:
- name: Download Java
command: "wget -q -O {{java_archive}} --no-check-certificate --no-cookies --header 'Cookie: oraclelicense=accept-securebackup-cookie' {{download_url}} creates={{java_archive}}"
- name: Unpack archive
command: "tar -zxf {{java_archive}} -C {{download_folder}} creates={{java_name}}"
- name: Fix ownership
file: state=directory path={{java_name}} owner=root group=root recurse=yes
- name: Make Java available for system
command: 'alternatives --install "/usr/bin/java" "java" "{{java_name}}/bin/java" 2000'
- name: Clean up
file: state=absent path={{java_archive}}
@Vijay4275
Copy link

I just made some minor changes and this works for me.

  • name: Creates Directory structure
    file:
    path: /opt/oracle
    state: directory
    owner: ec2-user
    group: ec2-user
    mode: 0755
    become: true

  • name: Creates Directory structure
    file:
    path: /opt/oracle/jdk1.8.0_131
    state: directory
    owner: ec2-user
    group: ec2-user
    mode: 0755
    become: true

  • name: Download Java
    get_url: url={{ jdk_tarball_url }} dest={{ java_archive }} headers="Cookie:' gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie'" validate_certs=no owner=root group=root mode=744

  • name: Unpack archive
    unarchive: copy=no src={{ download_folder }}/{{ jdk_archive }} dest={{download_folder}}

  • name: Fix ownership
    file: state=directory path={{java_name}} owner=root group=root recurse=yes

  • name: Make Java available for system by updating alternatives
    command: alternatives --install /usr/bin/java java {{ java_name }}/bin/java 2000
    become: true

  • name: Make Jar available for system by updating alternatives
    command: alternatives --install /usr/bin/jar jar {{ java_name }}/bin/jar 2
    become: true

  • name: Make Javac available for system by updating alternatives
    command: alternatives --install /usr/bin/javac javac {{ java_name }}/bin/javac 2
    become: true

  • name: Make Jar available for system by updating alternatives
    command: alternatives --set jar {{ java_name }}/bin/jar
    become: true

  • name: Make Javac available for system by updating alternatives
    command: alternatives --set javac {{ java_name }}/bin/javac
    become: true

  • name: Set systemwide oracle env file under /etc/profile.d/
    set_fact: remote_file_path={{profile_path}}/oracle_jdk.sh

Create a blank file

  • name: Create a new file {{ profile_path }}/oracle_jdk.sh
    file: path={{ remote_file_path }} state=touch

Check remote file

  • stat: path={{ remote_file_path }}
    register: file_path

Copy /etc/profile.d/oracle_jdk.sh with content

  • copy:
    content: |
    export JDK_HOME={{ java_name }}
    export JAVA_HOME={{ java_name }}
    export JRE_HOME={{ java_name }}/jre
    export PATH=$PATH:{{ java_name }}/bin:{{ java_name }}/jre/bin
    dest: /etc/profile.d/oracle_jdk.sh

Fix the executable permission

  • name: Set executable perission
    file: path={{profile_path}}/oracle_jdk.sh owner=root group=root mode=0555 state=file recurse=no

Export the env on th fly to make system wide change

  • name: Source the file {{profile_path}}/oracle_jdk.sh
    action: shell source /etc/profile

@ashokinfy7
Copy link

Could you please share Java installation by using tar file with Ansible playbook and update paths in all VMS?

@antra13
Copy link

antra13 commented Feb 5, 2019

I got this error on executing:
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["wget", "-q", "-O", "/opt/jdk-8u5-linux-x64.tar.gz", "--no-check-certificate", "--no-cookies", "--header", "Cookie: oraclelicense=accept-securebackup-cookie", "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz"], "delta": "0:00:04.139554", "end": "2019-02-05 05:39:55.111171", "msg": "non-zero return code", "rc": 8, "start": "2019-02-05 05:39:50.971617", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment