Sunday, September 10, 2017

Ansible - Java 8 Installation Paybook

A Ansible playbook for Java installation on CetOS/RedHat OS. You will need to replace "hosts", "remote_user", "download_url" with your own. (https://github.com/tonylixu/ansible/blob/master/playbooks/java/main.yml)
---
- hosts: target_host
  remote_user: user
  become: yes
  become_method: sudo
  vars:
    download_url: java_download_url
    download_folder: /opt
    install_folder: /usr/lib/jvm
    java_name: "{{install_folder}}/jdk1.8.0_144"
    java_archive: "{{download_folder}}/jdk-8u144-linux-x64.tar.gz"

  tasks:
  - name: Check if Java is installed
    command: "java -version"
    # Alwasy run in normal mode
    check_mode: no
    register: rpm_check
    # Force the annoying change message to go away
    changed_when: no
    ignore_errors: yes

  - name: Create "/usr/lib/jvm" folder
    command: "mkdir /usr/lib/jvm"
    when: rpm_check.rc != 0

  - name: Download Java
    command: "wget -q -O {{java_archive}} {{download_url}} creates={{java_archive}}"
    when: rpm_check.rc != 0

  - name: Unpack archive
    command: "tar -xzf {{java_archive}} -C {{install_folder}}"
    when: rpm_check.rc != 0

  - name: Fix ownership
    file: state=directory path={{java_name}} owner=root group=root recurse=yes
    when: rpm_check.rc != 0

  - name: Make Java available for system
    command: 'alternatives --install "/usr/bin/java" "java" "{{java_name}}/bin/java" 2000'
    when: rpm_check.rc != 0

  - name: Make jar available for system
    command: 'alternatives --install "/usr/bin/jar" "jar" "{{java_name}}/bin/jar" 2000'
    when: rpm_check.rc != 0

  - name: Make javac available for system
    command: 'alternatives --install "/usr/bin/javac" "javac" "{{java_name}}/bin/javac" 2000'
    when: rpm_check.rc != 0

  - name: Clean up
    file: state=absent path={{java_archive}}
    when: rpm_check.rc != 0

No comments: