-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusergen-csv.py
More file actions
74 lines (55 loc) · 3.03 KB
/
usergen-csv.py
File metadata and controls
74 lines (55 loc) · 3.03 KB
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
#!/usr/bin/env python3
"""
Script : usergen-csv.py
Description : Python script to convert csv user list to yml vars file.
Dependencies : ansible, python, ssh
Usage : python3 usergen-csv.py userlist.csv
Author : Fai Tao
Version : 0.5
Date : 12 May 2022
Change history (most recent first)
Date Who Comments
---- --- --------
12/05/22 FT Improved yml_header format to use single string
12/05/22 FT Added optional headers list
11/05/22 FT Changed to func
11/05/22 FT Improved error checking and simplified input file to csv format
03/10/20 FT Added positional argument parsing and error checking
03/10/20 FT Added interaction
02/10/20 FT Initial draft
"""
import csv
import argparse
import sys
def main():
'''
Function to convert csv file to Ansible yml vars file for adding users
'''
# Set up parser to look for command line arg and provide error handling
parser = argparse.ArgumentParser(description='Convert csv user list to yml vars file.')
parser.add_argument('filename', help='The csv user list to read.')
parser.add_argument('--version', '-v', action='version', version='%(prog)s 0.5')
args = parser.parse_args()
yml_header = "---\n\n# ANSIBLE VARS FILE FOR USERS\n# users:\n# - { username: 'some user', comment: 'some comment', group: 'some group' }\n\nusers:\n"
try:
with open(args.filename, 'r') as infile, open('usersvar.yml.out', 'w+') as writefile:
# headers = ['username', 'comment', 'gid'] # You can specify headers here and leave it out in the cvs file
# csv_data = csv.DictReader(infile, headers) # But you must specify it here as an extra arg. Uncomment these 2 lines if the csv file does not have headers.
csv_data = csv.DictReader(infile) # Comment out this line if the csv files does not contain headers
# writefile.write(f"---\n\n# ANSIBLE VARS FILE FOR USERS\n# users:\n# - {{ username: 'some user', comment: 'some comment', group: 'some group' }}\n\nusers:\n")
writefile.write(yml_header)
for line in csv_data:
if (not line['gid']) and (not line['comment']):
writefile.write(f" - {{ username: '{line['username']}' }}\n")
elif not line['comment']:
writefile.write(f" - {{ username: '{line['username']}', group: '{line['gid']}' }}\n")
elif not line['gid']:
writefile.write(f" - {{ username: '{line['username']}', comment: '{line['comment']}' }}\n")
else:
writefile.write(f" - {{ username: '{line['username']}', comment: '{line['comment']}', group: '{line['gid']}' }}\n")
except FileNotFoundError as err:
print(f"No such file: '{args.filename}'. Please provide csv user list. Exiting")
sys.exit(2)
return writefile
if __name__=="__main__":
main()