ansible inventory

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:
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
#!/usr/bin/env python
  
import  sqlite3
import  sys
try :
     import  json
except  ImportError:
     import  simplejson as json
  
dbname  =  '/etc/ansible/books.db'
  
def  grouplist(conn):
  
     inventory  = {}
  
  
     inventory[ 'local' =  '127.0.0.1'  ]
  
     cur  =  conn.cursor()
     cur.execute( "SELECT type, name FROM hosts ORDER BY 1, 2" )
  
     for  row  in  cur.fetchall():
         group  =  row[ 'type' ]
         if  group  is  None :
             group  =  'ungrouped'
          
  
         if  not  group  in  inventory:
             inventory[group]  =  {
                 'hosts'  : []
             }
         inventory[group][ 'hosts' ].append(row[ 'name' ])
  
     cur.close()
     print  json.dumps(inventory, indent = 4 )
  
def  hostinfo(conn, name):
  
     vars  =  {}
  
     cur  =  conn.cursor()
     cur.execute( "SELECT COUNT(*) FROM hosts WHERE name=?" , (name, ))
  
     row  =  cur.fetchone()
     if  row[ 0 = =  0 :
         print  json.dumps({})
         sys.exit( 0 )
  
     # Inject some variables for all hosts
     vars  =  {
         'admin'          'Jane Jolie' ,
         'datacenter'     1
     }
  
     if  'ldap'  in  name.lower():
         vars [ 'baseDN' =  'dc=mens,dc=de'
  
  
     print  json.dumps( vars , indent = 4 )
  
  
if  __name__  = =  '__main__' :
     con  =  sqlite3.connect(dbname)
     con.row_factory = sqlite3.Row
  
     if  len (sys.argv)  = =  2  and  (sys.argv[ 1 = =  '--list' ):
         grouplist(con)
     elif  len (sys.argv)  = =  3  and  (sys.argv[ 1 = =  '--host' ):
         hostinfo(con, sys.argv[ 2 ])
     else :
         print  "Usage: %s --list or --host <hostname>"  %  sys.argv[ 0 ]
         sys.exit( 1 )
  
     con.close()

测试

ansible -i test01.py xx -m shell -a "uname -a"


具体参考 

http://rfyiamcool.blog.51cto.com/1030776/1416808/

http://noops.me/?p=1446&utm_source=tuicool&utm_medium=referral


更新

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
# !/usr/bin/env python
# coding:utf-8
import  sqlite3
import  sys
 
try :
     import  json
except  ImportError:
     import  simplejson as json
 
def  grouplist():
     inventory  =  {}
     inventory[ 'local' =  [ '127.0.0.1' ]
     sfile = '/home/python/books.txt'
     with  open (sfile, 'rb' ) as f:
         for  in  f.readlines():
             group = i.strip().split()[ 1 ]
             name = i.strip().split()[ 0 ]
             if  not  group  in  inventory:
                 inventory[group]  =  {
                     'hosts' : []
                 }
             inventory[group][ 'hosts' ].append(name)
         print  json.dumps(inventory, indent = 4 )
 
 
def  hostinfo(conn, 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 )
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
cat books.txt cxx代表组名
cxx  172.1 . 1.196  sc_tomcat  8005  8080
sxx  172.1 . 1.196  sc_tomcat  8006  8081
wxx  172.1 . 1.196  sc_tomcat  8007  8082
 
 
 
第三种
 
#从mysql hosts表生成最新的ansible hosts文件
import  os
import  sys
import  commands
import  MySQLdb
import  json
 
def  Inventory(iplist1):
     #不用
     inventory  =  {}
     for  ip  in  iplist1:
         if  ip  in  dict .iterkeys():
             # print ip,dict[ip]
             group  =  dict [ip]
             if  not  group  in  inventory:
                 inventory[group]  =  {
                     'hosts' : []
                 }
             inventory[group][ 'hosts' ].append(ip)
     print  json.dumps(inventory, indent = 4 )
 
def  grouplist(conn, file ):
     inventory  =  {}
     #inventory['local'] = ['127.0.0.1']
     cur  =  conn.cursor()
     cur.execute( "SELECT ip,env from hosts ORDER BY 1, 2" )
     for  row  in  cur.fetchall():
         group  =  row[ 1 ]
         if  len (group.split( ',' ))> 1 :
             for  in  range ( len (group.split( ',' ))):
                 pgroup  =  group.split( ',' )[i]
                 ip  =  row[ 0 ]
                 if  not  pgroup  in  inventory:
                     inventory[pgroup]  =  {
                         'hosts' : []
                     }
                 inventory[pgroup][ 'hosts' ].append(ip)
         else :
             if  group  is  None :
                 group  =  'ungrouped'
 
             if  not  group  in  inventory:
                 inventory[group]  =  {
                     'hosts' : []
                 }
             inventory[group][ 'hosts' ].append(row[ 0 ])
 
     cur.close()
 
     anum  =  0
     for  num  in  inventory.items():
         anum   + =   len (num[ 1 ][ 'hosts' ])
     print  anum
 
 
     =  open ( file 'w' )
     f.write(json.dumps(inventory, indent = 4 ))
     f.close()
 
if  __name__  = =  '__main__' :
     conn  =  MySQLdb.connect( "172.1.1.197" "root" "root" "cmdb" )
     file  =  'mysql_inventory'
     grouplist(conn, file )



本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1917739,如需转载请自行联系原作者
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
网络安全 数据安全/隐私保护
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
172 0
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
|
JSON 缓存 Shell
Ansible Dynamic Inventory 管理阿里云资源
![屏幕快照 2019-01-14 下午10.40.53.png](http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/934598a35f0ad25a6d21979547642603.png) Ansible 作为一款主流的自动化运维工具,可同时操作一台或者多台机器。当机器逐渐增多,机器角色越来越复杂多样时,分组管理就变得必不可少了。Ans
3133 0
|
数据格式 Python JSON
Ansible Inventory指北进阶
Ansible的inventory文件定义了它要操作的一些主机,它可以通过inventory对这些主机进行操控。默认的inventory文件是/etc/ansible/hosts 。
|
缓存 网络协议 API