使用Ansible自动配置JDK环境

时间:2021-09-27 01:46:28

1、首先安装好Ansible环境,具体步骤请见Ansible安装

2、先创建hosts文件(为后面编写脚本安装JDK做铺垫)

[root@localhost /]# vi hosts

[jdktest]
192.168.186.134 ansible_ssh_user=guxiong ansible_ssh_pass=private

注:记得/etc/ansible/hosts也要加入

3、创建该层级的文件目录

├── roles
├── jdk-install
│ ├── default
│ ├── files #jdk压缩包存放目录
│ │ └── jdk-7u79-linux-x64.tar.gz
│ ├── handler
│ ├── meta
│ ├── tasks #部署安装流程yml文件目录
│ │ └── main.yml
│ ├── templates
│ │ └── set_jdk.sh #配置目标机器的Java环境变量脚本
│ └── vars
│ └── main.yml #记录使用到的变量
├──jdk-install.yml
├──hosts
├──jdk-install.sh

4、编辑tasks下面的main.yml

  - name: copy jdk to remote host
copy: src=jdk-7u79-linux-x64.tar.gz dest=~
- name: unzip jdk
shell: tar -zxf jdk-7u79-linux-x64.tar.gz
- name: creat soft link
shell: ln -s ~/jdk1.7.0_79 jdk
- name: set jdk_env copy use template
template: src=set_jdk.sh dest=~/set_jdk.sh
- name: execute script to set jdkenv
shell: sh set_jdk.sh
- name: source bash_profile
shell: source ~/.bash_profile
- name: remove files
shell: rm jdk-7u79-linux-x64.tar.gz set_jdk.sh

5、编辑templates下的set_jdk.sh

#!/bin/bash

echo "export JAVA_HOME=${HOME}/jdk" >> ~/.bash_profile
echo 'export CLASSPATH=.:${JAVA_HOME}/lib' >> ~/.bash_profile
echo 'export PATH=${JAVA_HOME}/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

6、将使用到的变量编辑在vars下的main.yml

user: guxiong
host_cluster: jdktest

7、jdk-install.yml

- hosts: "{{ host_cluster }}"
gather_facts: no
vars:
  user: "{{ user }}"
roles:
  - jdk-install

8、ansible-playbook安装

[root@localhost /]# ansible-playbook jdk-install.yml --extra-var "host_cluster=jdktest user=guxiong"

PLAY [jdktest] **************************************************************** 

TASK: [jdk-install | copy jdk to remote host] *********************************
changed: [192.168.186.134] TASK: [jdk-install | unzip jdk] ***********************************************
changed: [192.168.186.134] TASK: [jdk-install | creat soft link] *****************************************
changed: [192.168.186.134] TASK: [jdk-install | set jdk_env copy use template] ***************************
changed: [192.168.186.134] TASK: [jdk-install | execute script to set jdkenv] ****************************
changed: [192.168.186.134] TASK: [jdk-install | source bash_profile] *************************************
changed: [192.168.186.134] TASK: [jdk-install | remove files] ********************************************
changed: [192.168.186.134] PLAY RECAP ********************************************************************
192.168.186.134 : ok=7 changed=7 unreachable=0 failed=0

8、为了方便编写安装脚本

[root@localhost /]# vi jdk-install.sh

#!/bin/bash

set -e
source ~/.bash_profile
ansible-playbook jdk-install.yml -i ./hosts -e "host_cluster=${host_cluster} user=${user}"       注:${host_cluster} 为执行脚本时需要传入的服务组名,${user}为用户名

[root@localhost /]# ./jdk-install.sh jdktest guxiong

结果一样能安装成功

PLAY [] *********************************************************************** 

TASK: [jdk-install | copy jdk to remote host] *********************************
changed: [192.168.186.134] TASK: [jdk-install | unzip jdk] ***********************************************
changed: [192.168.186.134] TASK: [jdk-install | creat soft link] *****************************************
changed: [192.168.186.134] TASK: [jdk-install | set jdk_env copy use template] ***************************
changed: [192.168.186.134] TASK: [jdk-install | execute script to set jdkenv] ****************************
changed: [192.168.186.134] TASK: [jdk-install | source bash_profile] *************************************
changed: [192.168.186.134] TASK: [jdk-install | remove files] ********************************************
changed: [192.168.186.134] PLAY RECAP ********************************************************************
192.168.186.134 : ok=7 changed=7 unreachable=0 failed=0