ansible inventory

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 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,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
网络安全 数据安全/隐私保护
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
99 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
2929 0
|
数据格式 Python JSON
Ansible Inventory指北进阶
Ansible的inventory文件定义了它要操作的一些主机,它可以通过inventory对这些主机进行操控。默认的inventory文件是/etc/ansible/hosts 。
1933 0
|
缓存 网络协议 API