主机hosts
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
devhosts.py#!/usr/bin/python# coding:utf-8import 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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
devansible.py#!/usr/bin/python# coding:utf-8import 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
|
1
2
3
4
5
|
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
手动安装
|
1
2
3
4
|
useradd zabbix -s /sbin/nologin
touch /var/log/zabbix_agentd.log
chmod 777 /var/log/zabbix_agentd.log
sudoers |
最后,把主机hosts文件插入数据库
本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1970973,如需转载请自行联系原作者
本文介绍了一种使用Ansible进行批量部署的方法,包括通过解析主机列表文件生成动态inventory,并自动创建用于不同服务(如Tomcat、Redis等)的Ansible Playbook。此外,还提供了如何配置和运行Playbook的具体步骤。
2194

被折叠的 条评论
为什么被折叠?



