Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@robi56

robi56 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 }}"  

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

This comment has been minimized.

Show comment Hide comment
@digicyc

digicyc 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.

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.

@faizan80

This comment has been minimized.

Show comment Hide comment
@faizan80

faizan80 Feb 8, 2017

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

faizan80 commented Feb 8, 2017

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

@jeanpaulazara

This comment has been minimized.

Show comment Hide comment

jeanpaulazara commented Mar 6, 2017

@abhisheksharma14

This comment has been minimized.

Show comment Hide comment
@abhisheksharma14

abhisheksharma14 Mar 18, 2017

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?

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

This comment has been minimized.

Show comment Hide comment
@pavansai8

pavansai8 Apr 25, 2017

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}}

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

This comment has been minimized.

Show comment Hide comment
@pavansai8

pavansai8 Apr 25, 2017

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": [

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

This comment has been minimized.

Show comment Hide comment
@girishkg

girishkg May 15, 2017

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

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

This comment has been minimized.

Show comment Hide comment
@Vijay4275

Vijay4275 Apr 4, 2018

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment