python3连接mysql获取ansible动态inventory脚本
ansible inventory 介绍
ansible inventory 是包含静态 inventory 和动态 inventory 两部分的,静态 inventory 指的是在文件中指定的主机和组,动态 inventory 指通过外部脚本获取主机列表,并按照 ansible 所要求的格式返回给 ansilbe 命令的。这部分一般会结合 cmdb 资管系统、云计算平台等获取主机信息。由于主机资源一般会动态的进行增减,而这些系统一般会智能更新。我们可以通过这些工具提供的 api 或者接入库查询等方式返回主机列表。
脚本地址:https://github.com/albertcqy/scripts/tree/master/ansible
脚本用法:readme.txt
1、脚本用法
bestpay用户 cd /tools/scripts/ansible
python3.6 invscript.py -h
1)、查询某个分组中包含哪些主机,oracle_nj_all为inventory_group.py中的组名
python3.6 invscript.py --group oracle_nj_all
2)、查询所有
python3.6 invscript.py --list
3)、查询某分组主机中主机名,脚本也可以在play-book中使用-i指定inventory
ansible -i invscript.py mysql_nj_all -m shell -a “hostname”
2、更新数据库主机列表,sql中最好对ip进行去除空格并排序,否则可能会有warning
inventory_group.py中格式:
mygrp2就是ansible使用的组名
‘ssh_user':‘root' root用户代表连到目标主机的用户。如果ansible server主机的test用户和目标的root用户有互信,那么ansible脚本在test用户下执行。
例子如下:
mygrp2 = {‘sql':""" select ‘1.1.3.8' as ips union all select ‘1.1.3.112' as ips union all select ‘1.1.3.113' as ips “”", ‘ssh_user':‘root'}
部分脚本内容:
#!/usr/bin/env python #-*- coding: utf-8 -*- # ========================================================================= """ -- file name : invscript.py -- purpose : 从mysql数据中动态获取主机列表,动态inventory,不用维护主机列表 -- date : 2020/01 -- author:陈晴阳 vervisons: -- 20200106 1.0,陈晴阳,实现了动态获取主机列表,按照默认互信方式获取主机信息。 -- 20200116 2.0,陈晴阳,增加--group参数,并统计各个分组主机个数;重构了group_all 所有主机按照各组设置的互信用户来抓信息;增加对ip地址排序功能。 """ # ========================================================================= import argparse import sys import json import settings import inventory_group as invgrp from connect_mysql import mysql_conn class dynamicinventory(object): def read_cli(self): parser = argparse.argumentparser() parser.add_argument('--host', nargs=1) parser.add_argument('--list', action='store_true') parser.add_argument('--group') self.options = parser.parse_args() def getitemlist(self): list_item = [] for item in dir(invgrp): if not item.startswith("__"): list_item.append(item) return list_item def getgrplist(self): list_grpinfo = [] list_item = self.getitemlist() for item in list_item: itemcontent = getattr(invgrp, item) tmp_dic = {} tmp_dic[item] = itemcontent list_grpinfo.append(tmp_dic) return list_grpinfo def get_groups(self): hostgroups = self.getgrplist() #allhost = [] for hostdic in hostgroups:#hostgroup为字典 for hostgroup in hostdic: #获取字典的key self.result[hostgroup] = {} v_sql = hostdic[hostgroup]['sql'] #获取sql v_ssh_user = hostdic[hostgroup]['ssh_user'] # 获取sql hosts = self.connection.execsql(v_sql) #print(hosts) # 构建每个分组 grp_host_list = [host[0] for host in hosts] grp_host_list = sorted(grp_host_list, key=lambda x: (int(x.split('.')[0]), int(x.split('.')[1]), int(x.split('.')[2]))) #排序 self.result[hostgroup]['hosts'] = grp_host_list self.result[hostgroup]['vars'] = {'ansible_ssh_user': v_ssh_user} #构建_meta,注意ip为元组,需要做个小转换ip[0] 需要字符串值 for ip in hosts: tmp_dic = {} tmp_dic['ansible_ssh_host'] = ip[0] self.result['_meta']['hostvars'][ip[0]] = tmp_dic # 构建group_all self.result[self.defaultgroup]['hosts']=[] self.result[self.defaultgroup]['children'] =self.getitemlist() return self.result def get_host(self,ipaddr): ip = '' for i in ipaddr: ip = i data = {'ansible_ssh_host': ip} return data def get_group_hosts(self,grpname): if grpname == 'group_all': allhosts = [] #查询出来所有的主机列表 hostgroups = self.getgrplist() for hostdic in hostgroups: # hostgroup为字典 for hostgroup in hostdic: # 获取字典的key v_sql = hostdic[hostgroup]['sql'] # 获取sql hosts = self.connection.execsql(v_sql) allhosts.extend([host[0] for host in hosts]) allhosts = set(allhosts) # 去重 allhosts = sorted(allhosts, key=lambda x: (int(x.split('.')[0]), int(x.split('.')[1]), int(x.split('.')[2]))) #排序 cnt = 0 for i in allhosts: print(cnt + 1, i) cnt = cnt + 1 print('group ' + grpname + ' total hosts:', cnt) else: txt_grp ='invgrp.'+grpname+"""['sql']""" v_sql = eval(txt_grp) #这里偷懒用了邪恶函数eval hosts = self.connection.execsql(v_sql) cnt = 0 for i in hosts: print(cnt + 1,i[0]) cnt = cnt + 1 print('group '+grpname+' total hosts:',cnt) def __init__(self): try: self.connection = mysql_conn(settings.my_usr, settings.my_pass, settings.my_ip, settings.my_port, settings.my_db) except exception as err: print("connect wrong", err) self.defaultgroup = 'group_all' self.options = none self.read_cli() self.result = {} self.result[self.defaultgroup] = {} self.result[self.defaultgroup]['hosts'] = [] self.result[self.defaultgroup]['vars'] = {'ansible_ssh_user': 'bestpay'} self.result['_meta'] = {} self.result['_meta']['hostvars'] = {} if self.options.host: data = self.get_host(self.options.host) print(json.dumps(data,indent=4)) elif self.options.list: data = self.get_groups() print(json.dumps(data,indent=4)) elif self.options.group: data = self.get_group_hosts(self.options.group) else: sys.exit("usage: --list or --host hostname or --group groupname") if __name__ == '__main__': dynamicinventory()
总结
以上所述是小编给大家介绍的python3连接mysql获取ansible动态inventory脚本,希望对大家有所帮助
下一篇: 详解mysql数据库如何开启慢查询日志