首页 > 代码库 > ansible安装zookeeper

ansible安装zookeeper

这个主要是给hbase用的,启动用户为hadoop

cat iphost.txt
n16 172.x.x.1
d17 172.x.x.2
d18 172.x.x.3

cd /etc/ansible/shell
cat iphost.py
#!/usr/bin/python
# coding:utf-8
import sys

try:
    import json
except ImportError:
    import simplejson as json

def grouplist():
    inventory = {}
    #inventory[‘local‘] = []
    sfile=‘/etc/ansible/shell/iphost.txt‘
    with open(sfile,‘rb‘) as f:
        for i in f.readlines():
            group=i.strip().split()[0]
            name=i.strip().split()[1]
            if not group in inventory:
                inventory[group] = {
                    ‘hosts‘: []
                }
            inventory[group][‘hosts‘].append(name)

        print json.dumps(inventory, indent=4)

    
def hostinfo(name):
    vars = {}
    vars = {
        ‘admin‘: ‘Jane Jolie‘,
        ‘datacenter‘: 1
    }
    print json.dumps(vars, indent=4)

if __name__ == ‘__main__‘:
    if len(sys.argv) == 2 and (sys.argv[1] == ‘--list‘):
        grouplist()
    elif len(sys.argv) == 3 and (sys.argv[1] == ‘--host‘):
        hostinfo(sys.argv[2])
    else:
        print "Usage: %s --list or --host <hostname>" % sys.argv[0]
        sys.exit(1)

cd /etc/ansible/zookeeper
tree
.
├── hosts
├── roles
│   └── http
│       ├── default
│       ├── files
│       │   ├── java.env
│       │   └── zookeeper.tar.gz
│       ├── handlers
│       ├── meta
│       │   └── test
│       ├── tasks
│       │   ├── main v1.yml
│       │   └── main.yml
│       ├── templates
│       │   └── zoo.cfg
│       └── vars
│           └── main.yml
└── site.yml

cd /etc/ansible/zookeeper/roles/http/tasks
[root@jk tasks]# ls
main v1.yml  main.yml
[root@jk tasks]# cat main.yml
- name: Stop zookeeper
  shell: ps -ef|grep zookeeper|grep -v grep|awk ‘{print $2}‘|xargs kill -9 >>/dev/null 2>&1
  ignore_errors: yes
- name: Copy zookeeper
  copy: src=http://www.mamicode.com/zookeeper.tar.gz dest=/tmp/zookeeper.tar.gz owner=hadoop group=hadoop"sudo -i -u hadoop nohup sh /home/hadoop/zookeeper/bin/zkServer.sh start &" >>/etc/rc.d/rc.local
  when: zookeepers.stdout|int == 0

cd /etc/ansible/zookeeper/roles/http/vars
cat main.yml 
ip1: 172.x.x.1
ip2: 172.x.x.2
ip3: 172.x.x.3

安装
ansible-playbook -i /etc/ansible/shell/iphost.py site.yml --extra-vars "myid=1 host=n16"
ansible-playbook -i /etc/ansible/shell/iphost.py site.yml --extra-vars "myid=2 host=d17"
ansible-playbook -i /etc/ansible/shell/iphost.py site.yml --extra-vars "myid=3 host=d18"

用ansible的shell sudo -i -u hadoop /home/hadoop/zookeeper/bin/zkServer.sh start
启动zkServer




本文出自 “python 运维” 博客,谢绝转载!

ansible安装zookeeper