Skip to content


installing JAVA with alternatives and ansible

This playbook will install and configure two Java version (7 and 8).

---
- hosts:
  vars:
   jbin: /usr/bin
   javas:
    java7:
      download: 'http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm'
      args: 'Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=acc'
      home: /usr/java/jdk1.7.0_79/bin
      unpackwith: yum -y localinstall /tmp/jdk-7u79-linux-x64.rpm
      priority: 1
    java8:
      download: 'http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.tar.gz'
      args: 'Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=ace'
      home: /usr/java/jdk1.8.0_111/bin
      unpackwith: gzip -d /tmp/jdk-8u111-linux-x64.tar.gz
      priority: 2

  tasks:
  - name: install wget
    yum: pkg=wget state=present

  - name: download Javas
    get_url:
      url: "{{ item.value.download }}"
      dest: /tmp
      headers: "{{ item.value.args }}"
      force: no
    with_dict: "{{ javas }}"

  - name: install Java7 & unpack Java8 packages
    command: "{{ item.value.unpackwith }}"
    with_dict: "{{ javas }}"

  - name: install Java8
    command: tar -xf /tmp/jdk-8u111-linux-x64.tar -C /usr/java

  - name: configure jar's
    command: 'alternatives --install {{ jbin }}/jar jar  {{ item.value.home }}/jar {{ item.value.priority }}'
    with_dict: "{{ javas }}"

  - name: configure java's
    command: 'alternatives --install {{ jbin }}/java java {{ item.value.home }}/java {{ item.value.priority }}'
    with_dict: "{{ javas }}"

  - name: configure javac's
    command: 'alternatives --install {{ jbin }}/javac javac {{ item.value.home }}/javac {{ item.value.priority }}'
    with_dict: "{{ javas }}"

  - name: remove java7 package
    file: path=/tmp/jdk-7u79-linux-x64.rpm state=absent

  - name: remove java8 package
    file: path=/tmp/jdk-8u111-linux-x64.tar state=absent

Ansible has modules “replacing” both tar and alternatives command that could be used instead of the command module and UNIX commands shown above.

Posted in LINUX.

Tagged with , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

WordPress Anti Spam by WP-SpamShield



Copyright © 2016 - 2017 Waldemar Mark Duszyk. All Rights Reserved. Created by Blog Copyright.