您的位置:首页 > 其它

ansible 部署zabbix_agentd(分应用)

2017-10-09 17:31 423 查看
主机hosts
tomcat  192.1.1.121
redis 192.1.1.121
mongodb  192.1.1.121
tomcat  192.1.1.122
tomcat  192.1.1.123
redis 192.1.1.124
mongodb  192.1.1.124
tomcat  192.1.1.124
other  192.1.1.125
tomcat  192.1.1.126
fastdfs 192.1.1.127
fastdfs 192.1.1.128
fastdfs 192.1.1.129
other   192.1.1.130
other   192.1.1.131
fastdfs 192.1.1.132
fastdfs 192.1.1.133


生成动态inventory
devhosts.py
#!/usr/bin/python
# coding:utf-8
import sys

try:
import json
except ImportError:
import simplejson as json

def grouplist():
inventory = {}
# inventory['local'] = ['127.0.0.1']
# sfile = '/etc/ansible/books.txt'
sfile = 'books.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)


生成批量部署ansible-playbook
devansible.py
#!/usr/bin/python
# coding:utf-8
import sys

try:
import json
except ImportError:
import simplejson as json

dfile = 'ansible.sh'

def grouplist():
inventory = {}
# inventory['local'] = ['127.0.0.1']
# sfile = '/etc/ansible/books.txt'
sfile = 'books.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)
return inventory

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

def ansiblelast():
inventory = grouplist()
keys = []
stringlist = []
for key, item in inventory.iteritems():
keys.append(key)
print keys
for key in keys:
stringa = 'ansible-playbook -i /etc/ansible/devhosts.py %s.yml --extra-vars ' \
'"host=%s"' % (key,key)
stringlist.append(stringa)
with open(dfile, 'wb') as f1:
for i in stringlist:
line = str(i) + "\n"
f1.write(line)

if __name__ == '__main__':
ansiblelast()


得到ansible.sh
ansible-playbook -i /etc/ansible/devhosts.py fastdfs.yml --extra-vars "host=fastdfs"
ansible-playbook -i /etc/ansible/devhosts.py mongodb.yml --extra-vars "host=mongodb"
ansible-playbook -i /etc/ansible/devhosts.py other.yml --extra-vars "host=other"
ansible-playbook -i /etc/ansible/devhosts.py redis.yml --extra-vars "host=redis"
ansible-playbook -i /etc/ansible/devhosts.py tomcat.yml --extra-vars "host=tomcat"
默认最基本的模板
ansible-playbook -i /etc/ansible/devhosts.py install.yml --extra-vars "host=all"

复制sudoers,可以准备几个sudoers
ansible -i /etc/ansible/devhosts.py all -m copy -a 'src=/etc/ansible/zabbix/sudoers dest=/etc/sudoers mode=440'

注意权限
chown -R zabbix.zabbix /usr/local/zabbix

手动安装
useradd zabbix -s /sbin/nologin
touch /var/log/zabbix_agentd.log
chmod 777 /var/log/zabbix_agentd.log
sudoers


最后,把主机hosts文件插入数据库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ansible