4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ansible で Java をインストール

Last updated at Posted at 2018-10-13

Ansible で Java をインストールするサンプル
※ 変数 java_version--extra-varsjava_version=11 の様に指定

defaults/main.yml
---
# 一時ディレクトリ
temp_dir: /var/tmp

# Java ディストリビューション
java_distributions:
  '11':
    url: https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.4%2B11/OpenJDK11U-jdk_x64_linux_hotspot_11.0.4_11.tar.gz

# Java インストールディレクトリ
java_install_dir: /usr/lib/jvm

# Java Home
java_home: "{{ java_install_dir }}/jdk-{{ java_version }}"
tasks/main.yml
---
- import_tasks: setup.yml
tasks/setup.yml
---
- name: Find installed Java.
  ansible.builtin.find:
    paths: "{{ java_install_dir }}"
    file_type: directory
    patterns: "jdk-{{ java_version }}*"
  register: installed_java

- name: Delete installed Java.
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  with_items:
    - "{{ installed_java.files }}"

- name: Download Java distribution file.
  ansible.builtin.get_url:
    url: "{{ java_distributions[java_version].url }}"
    dest: "{{ temp_dir }}"
  register: distribution_file

- name: Unarchive Java distribution file.
  ansible.builtin.unarchive:
    src: "{{ distribution_file.dest }}"
    dest: "{{ java_install_dir }}"
    remote_src: yes
    list_files: yes
    owner: root
    group: root
    mode: u=rwX,go=rX
  register: contents

- name: Create symbolic link.
  ansible.builtin.file:
    src: "{{ java_install_dir }}/{{ contents.files[0] | regex_replace('.*/(.+)/$', '\\1') }}"
    dest: "{{ java_home }}"
    state: link

- name: Make alternatives.
  community.general.alternatives:
    name: "{{ item }}"
    link: "/usr/bin/{{ item }}"
    path: "{{ java_home }}/bin/{{ item }}"
  with_items:
    - java
    - javac

- name: Delete Java distribution file.
  ansible.builtin.file:
    path: "{{ distribution_file.dest }}"
    state: absent
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?