ansible 部署zabbix_agentd(分应用)

本文介绍了一种使用Ansible进行批量部署的方法,包括通过解析主机列表文件生成动态inventory,并自动创建用于不同服务(如Tomcat、Redis等)的Ansible Playbook。此外,还提供了如何配置和运行Playbook的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主机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-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 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-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 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 in stringlist:
            line = str(i) + "\n"
            f1.write(line)
 
if __name__ == '__main__':
    ansiblelast()


得到ansible.sh

1
2
3
4
5
ansible-playbook -/etc/ansible/devhosts.py fastdfs.yml --extra-vars "host=fastdfs"
ansible-playbook -/etc/ansible/devhosts.py mongodb.yml --extra-vars "host=mongodb"
ansible-playbook -/etc/ansible/devhosts.py other.yml --extra-vars "host=other"
ansible-playbook -/etc/ansible/devhosts.py redis.yml --extra-vars "host=redis"
ansible-playbook -/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 -/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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值