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
i
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
i
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
f
=
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,如需转载请自行联系原作者