Skip to content
Snippets Groups Projects
Commit ddee39ca authored by (Service) CSIRT's avatar (Service) CSIRT
Browse files

Initial version, tested

parent fad846cc
No related branches found
No related tags found
No related merge requests found
## Description
An [Ansible](http://www.ansible.com) role to install Java8 JDK. This role works on Debian, Ubuntu and RedHat (CentOS) operation systems. Installation from source is also supported.
## Requirements
- Ansible >= 2.X
## Facts
- **java_installed**: fact to indicate if Java is installed on the host.
- **java_version_installed**: fact containing Java version installed on the host in a string.
## Role Variables
- **java_source**: flag to enable installation from source files instead from a repository.
- **debug**: flag to enable verbose mode within the role.
- **java_set_as_default**: flag to set the Java version installed by this role as default (default= `yes`).
### Debian
- **java_apt_repository**: Java source PPA for installation .
- **java_apt_repository_key**: PPA key.
- **java_cache_valid_time**: how many seconds is the apt cache valid.
- **java_deb_package**: name of the Debian package.
- **java_debconf_package_default**: name of the Debconf package to set default.
- **java_home**: Java home directory location.
- **java_state**: the package state.
### Debian/Ubuntu
- **java_apt_repository**: Java source PPA for installation .
- **java_cache_valid_time**: how many seconds is the apt cache valid.
- **java_deb_package**: name of the Debian package.
- **java_debconf_package_default**: name of the Debconf package to set default.
- **java_home**: Java home directory location.
- **java_license_version**: Oracle license version to be accepted.
- **java_state**: the package state.
### Redhat-only
- **java_dir_source**: Where to store the RPM files.
- **java_download_timeout**: download timeout in seconds.
- **java_home**: Java home directory location.
- **java_rpm_filename**: download destination filename.
- **java_rpm_url**: RPM source URL.
- **java_rpm_validate_certs**: enable/disable SSL certificate validation.
- **java_version_string**: string with Java version to verify installation against.
## Example Playbook
```yaml
# Generic with debug
- hosts: myServer
roles:
- role: java-8
debug: yes
java_source: yes
# Enable debug, install from source
- hosts: myServer
roles:
- role: java-8
debug: yes
java_source: yes
```
File added
---
# File: java-8/defaults/debian-ubuntu.yml
# Description:
# Default variables for Ubuntu distributions.
java_apt_repository: 'ppa:webupd8team/java'
java_cache_valid_time: 3600
java_deb_package: 'oracle-java8-installer'
java_debconf_package_default: 'oracle-java8-set-default'
java_home: "/usr/lib/jvm/java-8-oracle"
java_license_version: 'shared/accepted-oracle-license-v1-1'
java_state: latest
---
# File: java-8/defaults/debian.yml
# Description:
# Default variables for Debian-based distributions.
java_apt_repository: 'deb http://ppa.launchpad.net/linuxuprising/java/ubuntu bionic main'
java_apt_repository_key: 'EA8CACC073C3DB2A'
java_cache_valid_time: 3600
java_deb_package: 'oracle-java8-installer'
java_debconf_package_default: 'oracle-java8-set-default'
java_home: "/usr/lib/jvm/java-8-oracle"
java_license_version: 'shared/accepted-oracle-license-v1-1'
java_state: latest
---
# File: java-8/defaults/main.yml
# Description:
# Defaults main file
java_set_as_default: yes
java_source: no
java_source_url: "https://kypo-storage.ics.muni.cz/data/public/java/jdk-8u151-linux-x64.tar.gz"
---
# File: java-8/defaults/redhat.yml
# Description:
# Default variables for Redhat-based distributions.
java_dir_source: '/usr/local/src'
java_download_timeout: 60
java_rpm_filename: 'jdk-8u191_linux-x64.rpm'
java_home: '/usr/java/default'
java_rpm_url: 'http://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.rpm'
java_rpm_validate_certs: yes
java_version_string: 1.8.0_191
---
# File: java-8/tasks/check_env.yml
# Description:
# This task checks host envinronment
# and sets these facts:
# - Is Java already installed?
# - If so, which Java version is already installed?
- name: check if Java is already installed
shell: which java
register: java_task_installed
changed_when: java_task_installed.rc != 0
failed_when: no
# java_installed.rc=0 means installed
# java_installed.rc=1 means not installed
- name: set fact java_installed
set_fact:
java_installed={{ not java_task_installed.changed }}
- name: check installed Java version
shell: java -version 2>&1 | head -n 1 | awk '{ print $3 }' | awk -F '"' '{ print $2 }'
register: java_task_version
when: java_installed
changed_when: False
- name: set fact java_installed_version
set_fact:
java_version_installed="{{ java_task_version.stdout }}"
when: java_task_version is defined and java_task_version.stdout is defined
changed_when: False
- debug:
var="{{ item }}"
when: item is defined and debug | default(false)
with_items:
- java_installed
- java_task_installed
- java_task_version
- java_version_installed
tags: [ debug ]
---
# File: java-8/tasks/debug.yml
# Description:
# This task just prints
# debug information about vars.
#
- debug:
var="{{ item }}"
when: item is defined
with_items:
- java_cache_valid_time
- java_home
- java_installed
- java_apt_repository
- java_apt_repository_key
- java_license_version
- java_set_as_default
- java_state
- java_version_installed
- java_version_string
---
# File: java-8/tasks/installation/debian/main.yml
# Description:
# This task installs Oracle JDK into a system
# with Debian based OS.
- name: Debian -> ensure the APT repository key is present
apt_key:
id="{{ java_apt_repository_key }}"
keyserver=keyserver.ubuntu.com
state=present
when: java_apt_repository_key is defined
become: yes
- name: Debian -> ensure the APT repository is present
apt_repository:
repo="{{ java_apt_repository }}"
update_cache=yes
state=present
become: yes
- name: Debian -> set license as accepted
debconf:
name="{{ java_deb_package }}"
question="{{ java_license_version }}"
value='true'
vtype='select'
become: yes
- name: Debian -> ensure Java is installed
apt:
name="{{ java_deb_package }}"
state={{ java_state }}
cache_valid_time={{ java_cache_valid_time }}
update_cache=yes
register: java_task_apt_install
become: yes
- name: Debian -> set Java version as default
apt:
name="{{ java_debconf_package_default }}"
state=latest
register: java_task_set_default
when: java_set_as_default
become: yes
---
# File: java-8/tasks/installation/redhat/main.yml
# Description:
# This task installs Oracle JDK into a system
# with RedHat based OS.
- name: RedHat -> download Java RPM
get_url:
headers='Cookie:gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie'
dest="{{ java_dir_source }}/{{ java_rpm_filename }}"
url="{{ java_rpm_url }}"
validate_certs="{{ java_rpm_validate_certs }}"
timeout={{ java_download_timeout }}
force=no
register: java_task_rpm_download
until: java_task_rpm_download|succeeded
become: yes
- name: RedHat -> install RPM
yum:
name="{{ java_dir_source }}/{{ java_rpm_filename }}"
state=present
when: not java_task_rpm_download|skipped
become: yes
- name: RedHat -> set Java version as default
alternatives:
name="{{ item.executable }}"
link="/usr/bin/{{ item.executable }}"
path="{{ item.path }}/{{ item.executable }}"
with_items:
- { path: "{{ java_home }}/bin", executable: 'java' }
- { path: "{{ java_home }}/bin", executable: 'keytool' }
- { path: "{{ java_home }}/bin", executable: 'javac' }
- { path: "{{ java_home }}/bin", executable: 'javadoc' }
become: yes
when: (
java_set_as_default and
java_task_rpm_download is defined and
java_task_rpm_download|changed
) or (
java_set_as_default and
java_installed is defined and
java_installed and
java_version_installed is defined and
java_version_installed != java_version_string)
register: java_task_set_default
---
# File: java-8/tasks/installation/source/java-8-source.yml
# Description: This task downloads and installs
# Java source files.
- name: source -> create temporary directory
tempfile:
state: directory
register: temp_dir
- name: source -> get JAVA
get_url:
url: '{{ java_source_url }}'
dest: /tmp/java.tar.gz
validate_certs: no
- name: source -> unarchive JAVA
unarchive:
remote_src: yes
src: /tmp/java.tar.gz
dest: '{{ temp_dir.path }}'
- name: source -> create jvm directory
file:
path: /usr/lib/jvm
state: directory
- name: source -> deploy java directory
shell: 'mv {{ temp_dir.path }}/`ls {{ temp_dir.path }} | head -n 1` /usr/lib/jvm/java-8-oracle'
- name: source -> configure java
alternatives:
link: '/usr/bin/{{ item }}'
name: '{{ item }}'
path: '/usr/lib/jvm/java-8-oracle/bin/{{ item }}'
priority: 1
with_items:
- java
- javac
- name: source -> set JAVA_HOME variable
lineinfile:
create: yes
path: /root/.mavenrc
line: 'export JAVA_HOME=/usr/lib/jvm/java-8-oracle'
---
# File: java-8/tasks/installation/source/main.yml
# Description:
# This task installs Oracle JDK into a system
# from source.
- name: source -> install required Java packages
apt:
name: "{{ item }}"
state: latest
with_items:
- ca-certificates
- name: source -> check if Java is downloaded
stat:
path: /usr/lib/jvm/java-8-oracle
register: downloaded_java
- include: java-8-source.yml
become: yes
become_user: root
when: downloaded_java.stat.exists == False
---
# File: java-8/tasks/main.yml
# Description:
# Main task file
#
## Include variables for given OS family/distribution
- name: include OS family/distribution specific variables
include_vars: "{{ item }}"
with_first_found:
- "{{ role_path }}/defaults/{{ ansible_os_family | lower }}-{{ ansible_distribution | lower }}.yml"
- "{{ role_path }}/defaults/{{ ansible_os_family | lower }}.yml"
when: not java_source
tags: [ installation ]
## Check environment and print debug info
- name: check host environment
include: check_env.yml
- include: debug.yml
when: debug | default(false)
tags: [ debug ]
## Include task for given OS family/distribution
- name: include OS family/distribution specific task
include: "{{ item }}"
with_first_found:
- "installation/{{ ansible_os_family | lower }}/{{ ansible_distribution | lower }}_{{ ansible_distribution_version | lower }}.yml"
- "installation/{{ ansible_os_family | lower }}/{{ ansible_distribution | lower }}.yml"
- "installation/{{ ansible_os_family | lower }}/main.yml"
when: not java_source
tags: [ installation ]
## Include task to install Java from source if specified
- name: include task for source installation
include: installation/source/main.yml
when: java_source
tags: [ installation ]
- name: check host environment
include: check_env.yml
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment