ansible-playbook部署zookeeper集群
https://gitee.com/liangsircc/my-ansible-example/tree/main/zookeeper_cluster
目录结构
root@mq1:~/my-ansible-example/zookeeper_cluster
.
├── ansible.cfg
├── hosts
├── roles
│ └── zknode
│ ├── files
│ │ └── zoo.cfg
│ ├── tasks
│ │ └── main.yml
│ └── vars
│ └── main.yml
├── zk.retry
└── zk.yml
zk.yml
---
- hosts: zk
remote_user: root
roles:
- zknode
vars/main.yml
---
packages:
- https://archive.apache.org/dist/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
packdir: /apps/
filename:
zk: "{{ packages[0].partition('.tar')[0]|basename }}"
tasks/main.yml
---
- name: apt install packages
apt: name="openjdk-8-jdk"
when: ansible_distribution != "CentOS"
- name: yum install packages
dnf: name="java-1.8.0-openjdk"
when: ansible_distribution == "CentOS"
- name: create directory
file: path={{ packdir }} state=directory
- name: download zk packages
unarchive: src={{ packages[0] }} dest={{ packdir }} copy=no
register: var1
until: var1 is succeeded
retries: 5
- name: creat soft link
file: src={{ packdir }}{{ filename.zk }} dest={{ packdir }}/zookeeper state=link
- name: copy cluster config file
copy: src=zoo.cfg dest={{ packdir }}{{ filename.zk }}/conf/zoo.cfg
- name: create zk data directory
file: path={{ packdir }}{{ filename.zk }}/data state=directory
- name: set host cluster id
shell: "echo {{ ansible_eth0.ipv4.address.split('.')[-1] }} > {{ packdir }}{{ filename.zk }}/data/myid"
- name: start zk node
shell: "{{ packdir }}{{ filename.zk }}/bin/zkServer.sh start"
执行脚本
root@mq1:~/my-ansible-example/zookeeper_cluster
PLAY [zk] ********************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [10.0.0.113]
ok: [10.0.0.112]
ok: [10.0.0.111]
TASK [zknode : apt install packages] *****************************************************************************************************************************************************
ok: [10.0.0.111]
ok: [10.0.0.113]
ok: [10.0.0.112]
TASK [zknode : yum install packages] ****************************************************************************************************************************************************
skipping: [10.0.0.111]
skipping: [10.0.0.112]
skipping: [10.0.0.113]
TASK [zknode : create directory] *********************************************************************************************************************************************************
ok: [10.0.0.111]
ok: [10.0.0.113]
ok: [10.0.0.112]
TASK [zknode : download zk packages] *****************************************************************************************************************************************************
ok: [10.0.0.111]
changed: [10.0.0.113]
changed: [10.0.0.112]
TASK [zknode : creat soft link] **********************************************************************************************************************************************************
ok: [10.0.0.111]
changed: [10.0.0.112]
changed: [10.0.0.113]
TASK [zknode : copy cluster config file] *************************************************************************************************************************************************
changed: [10.0.0.111]
changed: [10.0.0.112]
changed: [10.0.0.113]
TASK [zknode : create zk data directory] *************************************************************************************************************************************************
changed: [10.0.0.111]
changed: [10.0.0.112]
changed: [10.0.0.113]
TASK [zknode : set host cluster id] ******************************************************************************************************************************************************
changed: [10.0.0.111]
changed: [10.0.0.113]
changed: [10.0.0.112]
TASK [zknode : start zk node] ************************************************************************************************************************************************************
changed: [10.0.0.111]
changed: [10.0.0.112]
changed: [10.0.0.113]
PLAY RECAP *******************************************************************************************************************************************************************************
10.0.0.111 : ok=9 changed=4 unreachable=0 failed=0
10.0.0.112 : ok=9 changed=6 unreachable=0 failed=0
10.0.0.113 : ok=9 changed=6 unreachable=0 failed=0
验证集群状态
|