角色功能说明:
- 该角色实现对客户端主机的多版本java环境部署,并设置默认的java环境
角色部署:
- 创建目录结构,创建软连接,关联默认变量文件
WorkDir=~/devops/ansible/os_init && cd ${WorkDir}
RoleName=java
mkdir -pv roles/${RoleName}/{defaults,files,handlers,meta,tasks,templates,vars}
ln -s ${WorkDir}/defaults_var.yml roles/${RoleName}/defaults/main.yml
- 创建角色任务
cat >roles/${RoleName}/tasks/main.yml<<EOF
---
- name: "创建 java 环境目录"
file:
path: /usr/local/java
state: directory
owner: root
group: root
mode: 0755
- name: "分发 java6 部署包"
copy:
src: ${WorkDir}/software/{{ java6_name }}
dest: /tmp
owner: root
group: root
mode: 0644
- name: "分发 java7 部署包"
copy:
src: ${WorkDir}/software/{{ java7_name }}
dest: /tmp
owner: root
group: root
mode: 0644
- name: "分发 java8 部署包"
copy:
src: ${WorkDir}/software/{{ java8_name }}
dest: /tmp
owner: root
group: root
mode: 0644
- name: "部署 java6"
shell:
bash /tmp/{{ java6_name }}
args:
chdir: /usr/local/java
creates: "{{ java6_dirname }}"
- name: "部署 java7"
unarchive:
src: /tmp/{{ java7_name }}
dest: /usr/local/java
remote_src: True
creates: /usr/local/java/{{ java7_dirname }}
- name: "部署 java8"
unarchive:
src: /tmp/{{ java8_name }}
dest: /usr/local/java
remote_src: True
creates: /usr/local/java/{{ java8_dirname }}
- name: "创建软连接 java6"
file:
path: /usr/local/java/java_1.6
src: /usr/local/java/{{ java6_dirname }}
state: link
- name: "创建软连接 java7"
file:
path: /usr/local/java/java_1.7
src: /usr/local/java/{{ java7_dirname }}
state: link
- name: "创建软连接 java8"
file:
path: /usr/local/java/java_1.8
src: /usr/local/java/{{ java8_dirname }}
state: link
- name: "创建环境文件 java6"
copy:
dest: /usr/local/java/java_1.6_env
content: "export JAVA_HOME=/usr/local/java/java_1.6\n\
export CLASSPATH=\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib\n\
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH\n"
owner: root
group: root
mode: 0444
- name: "创建环境文件 java7"
copy:
dest: /usr/local/java/java_1.7_env
content: "export JAVA_HOME=/usr/local/java/java_1.7\n\
export CLASSPATH=\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib\n\
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH\n"
owner: root
group: root
mode: 0444
- name: "创建环境文件 java8"
copy:
dest: /usr/local/java/java_1.8_env
content: "export JAVA_HOME=/usr/local/java/java_1.8\n\
export CLASSPATH=\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib\n\
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH\n"
owner: root
group: root
mode: 0444
- name: "权限统一"
file:
path: /usr/local/java
state: directory
recurse: yes
owner: root
group: root
- name: "设置系统默认java环境"
lineinfile:
path: /etc/profile
line: "source /usr/local/java/java_1.8_env"
EOF
- 创建任务playbook并执行
cat >os-init-7-${RoleName}.yml<<EOF
---
- hosts: all
remote_user: sudoler
gather_facts: true
become: yes
become_user: root
become_method: su
roles:
- ${RoleName}
EOF
ansible-playbook -i inventory/hosts os-init-7-${RoleName}.yml
注意事项:
- java部署包较大,通过网络分发耗时较长,建议在模版机上将相应的包直接下载到对应目录
- 如果要进行java包的升级,则:
-
- 1,将高版本的包上传到文件共享服务器
-
- 2,修改 defaults_var.yml 中相应信息
-
- 3,下载包到部署主机 ansible-playbook -i inventory/hosts os-init-1-*.yml
-
- 4,重新执行本角色,完成升级 ansible-playbook -i inventory/hosts os-init-7-*.yml
-
- 5,这个升级可以是降级
[TOC]