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用户下执行。
例子如下:
1
2
3
4
5
6
7
8
|
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' }
|
部分脚本内容:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#!/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脚本,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
原文链接:https://blog.csdn.net/u010033674/article/details/104005394