Skip to content

Instantly share code, notes, and snippets.

@andershedstrom
Created June 16, 2014 11:21
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • 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}}
@robi56
Copy link

robi56 commented Feb 7, 2016

--- 
 - hosts: webservers
   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_65_ansiblecheck"
    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-coo$
   - name: Unpack Archive
     command: "tar -zxf {{java_archive}} -C {{download_folder}} creates={{java_name}}"

i try by this
but it show the error

ERROR: Syntax Error while loading YAML script, installejava.yaml
Note: The error may actually appear before this position: line 14, column 16

   - name: Unpack Archive
     command: "tar -zxf {{java_archive}} -C {{download_folder}} creates={{java_name}}"
               ^
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they 
start a value. For instance:            

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"  

@digicyc
Copy link

digicyc commented Mar 6, 2016

If that's an exact copy & paste of your playbook then it looks like you're missing some things on the wget play to Download Java. You'll notice it's cut off at the end.

@faizan002
Copy link

How can I acquire a JDK download URL.
e.g. I need it for u77 version of jdk8

@jeanpaulazara
Copy link

jeanpaulazara commented Mar 6, 2017

@abhisheksharma14
Copy link

I am facing following error while using this playbook (modified accordingly):
fatal: [ec2-52-34-61-252.us-west-2.compute.amazonaws.com]: FAILED! => {"changed": false, "cmd": "alternatives --install /usr/bin/java java /opt/jdk1.8.0_05/bin/java 2000", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}

I have confirmed that the directories specified are present in the VM(SUSE Linux) but it is still throwing the same error

I am using following playbook link
Is there anything I am missing?

@pavansai8
Copy link

Hi, It is not working for me. Below are the logs
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}}

@pavansai8
Copy link

TASK [Download Java] ***********************************************************
task path: /etc/ansible/java_install.yaml:13
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/commands/command.py
<192.168.99.100> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.99.100> SSH: EXEC sshpass -d15 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o User=root -o ConnectTimeout=1000 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.99.100 '/bin/sh -c '"'"'( umask 77 && mkdir -p "echo $HOME/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868" && echo ansible-tmp-1493132074.22-2111796574868="echo $HOME/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868" ) && sleep 0'"'"''
<192.168.99.100> PUT /tmp/tmp59fFYe TO /root/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868/command.py
<192.168.99.100> SSH: EXEC sshpass -d15 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o User=root -o ConnectTimeout=1000 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.99.100]'
<192.168.99.100> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.99.100> SSH: EXEC sshpass -d15 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o User=root -o ConnectTimeout=1000 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.99.100 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868/ /root/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868/command.py && sleep 0'"'"''
<192.168.99.100> ESTABLISH SSH CONNECTION FOR USER: root
<192.168.99.100> SSH: EXEC sshpass -d15 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o Port=22 -o User=root -o ConnectTimeout=1000 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.99.100 '/bin/sh -c '"'"'/usr/bin/python /root/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868/command.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1493132074.22-2111796574868/" > /dev/null 2>&1 && sleep 0'"'"''
fatal: [192.168.99.100]: 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:00.388364",
"end": "2017-04-25 20:24:35.784464",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "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",
"_uses_shell": false,
"chdir": null,
"creates": "/opt/jdk-8u5-linux-x64.tar.gz",
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"rc": 6,
"start": "2017-04-25 20:24:35.396100",
"stderr": "",
"stdout": "",
"stdout_lines": [],
"warnings": [

@girishkg
Copy link

This is perfectly working for me.
This one is custom with tar.gz file and plenty of other variables needs cleanup.
Working on CentOS7 and RHEL7 but run at your own risk.

https://github.com/girishkg/ansible/blob/master/playbooks/oracle/oracle_jdk_1.8-playbook.yml

@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