-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
executable file
·49 lines (38 loc) · 1.54 KB
/
generator.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
#!/usr/bin/python
import sys
import os
import argparse
import errno
from jinja2 import Environment, FileSystemLoader
def ensure_dir(dirname):
"""
Ensure that a named directory exists; if it does not, attempt to create it.
"""
try:
os.makedirs(dirname)
except OSError, e:
if e.errno != errno.EEXIST:
raise
parser = argparse.ArgumentParser(description='Create ansible roles basic structure folder.')
#parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("-p", "--path", help="Path were the role is generated")
parser.add_argument("role", help="Name of the ansible role")
args = parser.parse_args()
#Create folder structure
folders_to_create = ["files","templates","tasks","handlers","vars","defaults","meta"]
if args.path is None:
args.path = os.getcwd() #current dir
for directory in folders_to_create:
if not os.path.exists("{0}/{1}/{2}".format(args.path,args.role,directory)):
os.makedirs("{0}/{1}/{2}".format(args.path,args.role,directory))
#Define basic
templates = ["defaults.j2","handlers.j2","tasks.j2","vars.j2"]
env = Environment(loader=FileSystemLoader('templates'))
output = env.get_template('README.md.j2').render()
with open("{0}/{1}/README.md".format(args.path, args.role), "wb") as fh:
fh.write(output)
for template in templates:
output = env.get_template(template).render()
# to save the results
with open("{0}/{1}/{2}/main.yaml".format(args.path,args.role,template[:-3]), "wb") as fh:
fh.write(output)