每一个软件或集群都创建一个单独的角色。
mkdir -pv ansible/roles/{jdk,hadoop,spark}/{tasks,files,templates,meta,handlers,vars}
创建JDK role
pwd /root/ansible_playbooks/roles/jdk7 ls files handlers meta tasks templates vars
1、上传软件包
将jdk-8u80-linux-x64.gz上传到files目录下
2、编写tasks
vi tasks/main.yml - name: mkdir file: path=/usr/java state=directory mode=0755 - name: unzip unarchive: src={{jdk_package_name}} dest=/usr/local/java/ - name: set env lineinfile: dest={{env_file}} insertafter="{{item.position}}" line="{{item.value}}" state=present with_items: - {position: EOF, value: " "} - {position: EOF, value: "export JAVA_HOME=/usr/local/java/{{jdk_version}}"} - {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"} - name: enforce env shell: source {{env_file}}
3. 编写vars
vi vars/main.yml jdk_package_name: jdk-8u80-linux-x64.gz env_file: /etc/profile jdk_version: jdk1.8.0_80
4. playbook角色
vi jdk.yml - hosts: jdk remote_user: root roles: - jdk
ansible-playbook jdk.yml