Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java role #59

Merged
merged 19 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/molecule-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Test install_java

# yamllint disable-line rule:truthy
on:
pull_request:
paths:
- "molecule_configs/*"
- "roles/install_java/**"
- ".github/workflows/molecule.yml"
- ".github/workflows/molecule-java.yml"

jobs:
molecule-java:
uses: ./.github/workflows/molecule.yml
with:
tests-path: ansible_collections/mirsg/infrastructure/roles/install_java
24 changes: 24 additions & 0 deletions roles/install_java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Ansible Role: mirsg.infrastructure.install_java

A role to install and configure Java.

## Role Variables

`java_profile_d`: Defaults to "/etc/profile.d".

`java_home`: Defaults to "/usr/lib/jvm/jre".

`java_package`: Defaults to "java-1.8.0".

`java_package_version`: Defaults to "devel".

## Example Playbook

Including an example of how to use your role (for instance, with variables
passed in as parameters) is always nice for users too:

```yaml
- hosts: servers
roles:
- { role: mirsg.infrastructure.install_java }
p-j-smith marked this conversation as resolved.
Show resolved Hide resolved
```
5 changes: 5 additions & 0 deletions roles/install_java/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
java_profile_d: /etc/profile.d
java_home: /usr/lib/jvm/jre
java_package: java-1.8.0-openjdk
java_package_version: devel
3 changes: 3 additions & 0 deletions roles/install_java/molecule/centos7/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# test this scenario from the roles/install_java directory with the command
# molecule --base-config ../../molecule_configs/centos7_base_config.yml test --scenario centos7
7 changes: 7 additions & 0 deletions roles/install_java/molecule/resources/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Install Java
hosts: all
become: true
gather_facts: true
roles:
- role: mirsg.infrastructure.install_java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this empty file is required otherwise molecule complains that the inventory doesn't exist

14 changes: 14 additions & 0 deletions roles/install_java/molecule/resources/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Prepare - install sudo and Java 11
hosts: all
gather_facts: true
tasks:
- name: Install sudo
ansible.builtin.package:
name: sudo
state: present

- name: Install Java 11 before the role installs Java 8
ansible.builtin.package:
name: java-11-openjdk-devel
state: present
16 changes: 16 additions & 0 deletions roles/install_java/molecule/resources/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Verify correct java version is selected
hosts: all
tasks:
- name: Get Java version
ansible.builtin.shell:
cmd: |
set -o pipefail
java -version 2>&1 | grep version | awk '{print $3}' | sed 's/"//g'
Comment on lines +7 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the output from java -version is:

openjdk version "1.8.0_402"
OpenJDK Runtime Environment (build 1.8.0_402-b06)
OpenJDK 64-Bit Server VM (build 25.402-b06, mixed mode)

The output from the command on line 9 is:

1.8.0_402

register: java_version
changed_when: false

- name: Check the java version is correct
ansible.builtin.assert:
that:
- java_version.stdout | split("_") | first is version('1.8.0')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the _402 from 1.8.0_402 (as we only specify that 1.8.0 should be installed, not 1.8.0_402)

3 changes: 3 additions & 0 deletions roles/install_java/molecule/rocky9/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# test this scenario from the roles/install_java directory with the command
# molecule --base-config ../../molecule_configs/rocky9_base_config.yml test --scenario rocky9
23 changes: 23 additions & 0 deletions roles/install_java/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Ensure correct Java version is installed - {{ java_package }}
ansible.builtin.package:
name: "{{ java_package }}-{{ java_package_version }}"
state: installed

- name: Set JAVA_HOME through shell script
ansible.builtin.template:
src: "java_home.sh.j2"
dest: "{{ java_profile_d }}/java_home.sh"
mode: "0644"
when: java_home is defined and java_home != ''

- name: Get info for java package directory
ansible.builtin.stat:
path: "/usr/lib/jvm/{{ java_package }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/usr/lib/jvm/{{ java_package }} is a symlink. We need to use the full path to the target when selecting the version to use with alternatives

register: java_package_info

- name: Set the default java version - {{ java_package_info.stat.lnk_source }}
community.general.alternatives:
name: java
path: "{{ java_package_info.stat.lnk_source }}/jre/bin/java"
state: selected
1 change: 1 addition & 0 deletions roles/install_java/templates/java_home.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export JAVA_HOME="{{ java_home }}"
Loading