1. 準備工作

1.1 主機列表

IP

主機名

內存(GB)

CPU核數

磁盤

操作系統

CPU架構

10.0.0.13

arc-pro-dc01

16

1

500GB

CentOS 7.9.2009

x86_64

10.0.0.14

arc-pro-dc02

16

1

500GB

CentOS 7.9.2009

x86_64

10.0.0.15

arc-pro-dc03

16

1

500GB

CentOS 7.9.2009

x86_64

10.0.0.16

arc-pro-dc04

8

2

500GB

CentOS 7.9.2009

x86_64

10.0.0.17

arc-pro-dc05

8

2

500GB

CentOS 7.9.2009

x86_64

10.0.0.18

arc-pro-dc06

8

2

500GB

CentOS 7.9.2009

x86_64

10.0.0.19

arc-pro-dc07

8

2

500GB

CentOS 7.9.2009

x86_64

10.0.0.20

arc-pro-dc08

8

2

500GB

CentOS 7.9.2009

x86_64

10.0.0.21

arc-pro-dc09

8

2

500GB

CentOS 7.9.2009

x86_64

1.2 已安裝服務

版本

arc-pro-dc01

arc-pro-dc02

arc-pro-dc03

arc-pro-dc04

arc-pro-dc05

arc-pro-dc06

arc-pro-dc07

arc-pro-dc08

arc-pro-dc09

Ansible

2.9.27

説明:

  • 每個服務器的 IP 均是靜態的
  • 每個服務器的防火牆都已關閉
  • 每個服務器的 SELINUX 已經禁用
  • 每個服務器均存在一個管理員用户 admin,該用户可以免密碼執行 sudo 命令;
  • 在 arc-pro-dc01 機器上,可以使用 admin 用户免密碼 ssh 到其他機器;
  • 服務器之間的時間同步;
  • 所有操作均使用 admin 用户完成;
  • 每個服務的 /etc/hosts 文件均包含如下內容:
10.0.0.13 arc-pro-dc01
10.0.0.14 arc-pro-dc02
10.0.0.15 arc-pro-dc03
10.0.0.16 arc-pro-dc04
10.0.0.17 arc-pro-dc05
10.0.0.18 arc-pro-dc06
10.0.0.19 arc-pro-dc07
10.0.0.20 arc-pro-dc08
10.0.0.21 arc-pro-dc09

1.3 集羣規劃

版本

arc-pro-dc01

arc-pro-dc02

arc-pro-dc03

arc-pro-dc04

arc-pro-dc05

arc-pro-dc06

arc-pro-dc07

arc-pro-dc08

arc-pro-dc09

JDK

8u461

1.4 下載安裝包

JDK 8 下載頁面:

  • 8u211以及之後版本的下載頁面:https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html
  • 8u202以及更早的版本下載頁面:https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html

該網站需要註冊登錄後才可以下載JDK。下載 jdk-8u461-linux-x64.tar.gz。

ansible生產實踐三 安裝jdk_51CTO博客_java

使用 admin 用户登錄 arc-pro-dc01 機器,創建目錄:/home/admin/ansible/jdk

將 jdk-8u461-linux-x64.tar.gz 上傳至 arc-pro-dc01 機器的 /home/admin/ansible/jdk 目錄下。

2. Ansible 文件

2.1 Ansible 目錄結構

説明:在 arc-pro-dc01 機器上,執行 ansible 命令的基礎目錄為 /home/admin/ansible

$ tree /home/admin/ansible/
/home/admin/ansible/
├── ansible.cfg
├── hosts
└── jdk
    ├── install_jdk.yml
    └── jdk-8u461-linux-x64.tar.gz

2.2 ansible.cfg

[defaults]
inventory=./hosts
host_key_checking=False

2.3 hosts

[cluster]
arc-pro-dc01
arc-pro-dc02
arc-pro-dc03
arc-pro-dc04
arc-pro-dc05
arc-pro-dc06
arc-pro-dc07
arc-pro-dc08
arc-pro-dc09

2.4 install_jdk.yml

---
- name: Install JDK 8
  hosts: cluster
  become: true
  gather_facts: false
  vars:
    admin_user: admin
    jdk_tarball: "jdk-8u461-linux-x64.tar.gz"
    jdk_install_dir: "/usr/java"
    jdk_dirname: "jdk1.8.0_461" # 解壓後的目錄名
    jdk_symlink: "jdk"
    env_file: "/etc/profile.d/arc-env-jdk.sh"

  tasks:
    - name: Ensure JDK install directory exists
      file:
        path: "{{ jdk_install_dir }}"
        state: directory
        mode: '0755'
    - name: Remove jdk dir and create new
      shell: |
        rm -rf {{ jdk_install_dir }}/{{ jdk_symlink }} {{ jdk_install_dir }}/{{ jdk_dirname }}
        mkdir -p {{ jdk_install_dir }}/{{ jdk_dirname }}
    - name: Extract JDK tarball
      unarchive:
        src: "{{ jdk_tarball }}"
        dest: "{{ jdk_install_dir }}/{{ jdk_dirname }}"
        remote_src: no
        extra_opts:
          - "--strip-components=1" # 去掉壓縮包最頂層目錄,直接解壓到目標
    - name: Create jdk symlink
      file:
        src: "{{ jdk_dirname }}"
        dest: "{{ jdk_install_dir }}/{{ jdk_symlink }}"
        state: link
    - name: Ensure environment file exists
      file:
        path: "{{ env_file }}"
        state: touch # 文件存在則不改內容,不存在則創建空文件
        owner: "{{ admin_user }}"
        group: "{{ admin_user }}"
        mode: '0755'
    - name: Set JAVA_HOME and update PATH
      copy:
        dest: "{{ env_file }}"
        content: |
          export JAVA_HOME={{ jdk_install_dir }}/{{ jdk_symlink }}
          export PATH=$JAVA_HOME/bin:$PATH
    - name: Print java version
      shell: |
        source {{ env_file }}
        java -version
      args:
        executable: /bin/bash
      register: java_version
      ignore_errors: yes
    - name: Show java version output
      debug:
        msg: "{{ java_version.stderr_lines }}"

3. 安裝

在 arc-pro-dc01(Ansible 管理節點) 服務器執行

$ pwd
/home/admin/ansible

$ ansible-playbook jdk/install_jdk.yml

# 輸出
TASK [Show java version output] *********************************************
ok: [arc-pro-dc01] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc05] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc02] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc03] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc04] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc06] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc08] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc07] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}
ok: [arc-pro-dc09] => {
    "msg": [
        "java version \"1.8.0_461\"",
        "Java(TM) SE Runtime Environment (build 1.8.0_461-b11)",
        "Java HotSpot(TM) 64-Bit Server VM (build 25.461-b11, mixed mode)"
    ]
}

PLAY RECAP *******************************************************************************************************
arc-pro-dc01               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc02               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc03               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc04               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc05               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc06               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc07               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc08               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
arc-pro-dc09               : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0