1. 在hosts文件添一个group,里面是你需要安装jdk的ip,如:
[newhosts]
192.168.2.155 ansible_ssh_user=hadoop ansible_ssh_pass=hadoop ansible_sudo_pass=hadoop
192.168.2.159 ansible_ssh_user=hadoop ansible_ssh_pass=hadoop ansible_sudo_pass=hadoop
2. 创建层级结构
├── 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
3. 编辑tasks下面的main.yml
- name: copy jdk to remote host copy: src=jdk-7u80-linux-i586.tar.gz dest=~ - name: unzip jdk shell: tar -zxf jdk-7u80-linux-i586.tar.gz - name: creat soft link shell: ln -s ~/jdk1.7.0_80 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-7u80-linux-i586.tar.gz set_jdk.sh
4. 编辑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
5. 将使用到的变量编辑在vars下的main.yml
user: hadoop
host_cluster: newhosts
6. jdk-install.yml
- hosts: "{{ host_cluster }}" gather_facts: no vars: user: "{{ user }}" roles: - jdk
7. ansible-playbook安装
[hadoop@Ansible1 ansible]$ ansible-playbook jdk-install.yml