-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook_init.py
58 lines (48 loc) · 1.79 KB
/
playbook_init.py
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
#!/usr/bin/env python
import os
import yaml
import optparse
def get_options():
usage = "usage: %prog [options]"
OptionParser = optparse.OptionParser
parser = OptionParser(usage)
parser.add_option("-s","--site",action="store",type="string",\
dest="site",default="site.yml",help="playbook site file")
options,args = parser.parse_args()
return options,args
if __name__ == "__main__":
options,args = get_options()
workspace = "." if os.path.dirname(options.site) == "" else os.path.dirname(options.site)
common = ["files","templates","tasks","handlers","vars","defaults","meta"]
with open(options.site,"r") as f:
site = f.read()
site_dict = yaml.load(site)
###########init roles###################################
for host in site_dict:
try:
for i in host["roles"]:
try:
role = i["role"]
except:
role = i
for path in common:
mkdir=workspace+"/"+"roles"+"/"+role+"/"+path
if not os.path.exists(mkdir):
os.makedirs(mkdir)
if path != "files" and path != "templates":
os.mknod(mkdir+"/main.yml")
except:
os.mknod(workspace+"/"+host["include"])
#############init variable################################
vardir = workspace + "/group_vars/"
if not os.path.exists(vardir):
os.makedirs(vardir)
os.mknod(vardir+"all")
#############init hosts###################################
for host in site_dict:
try:
with open(workspace+"/hosts","a+") as f:
f.write("["+host["hosts"]+"]\n")
f.flush()
except:
pass