-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusergen5.py
More file actions
57 lines (44 loc) · 1.72 KB
/
usergen5.py
File metadata and controls
57 lines (44 loc) · 1.72 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
#!/usr/bin/env python3
'''
Script : usergen5.py
Description : Converts text list of users to yml
Dependencies : python
Usage : ./usergen5.py userfile.txt
Author : Fai Tao
Version : 0.2
Date : 06 Oct 2020
Change history (most recent first)
Date Who Comments
---- --- --------
27/10/20 FT Check data is clean
08/10/20 FT Converted to functions
02/10/20 FT Initial draft
'''
import argparse
import sys
def get_args():
parser = argparse.ArgumentParser(description='Convert user list to yml.')
parser.add_argument('filename', help='The user list to read.')
parser.add_argument('--version', '-v', action='version', version='%(prog)s 0.1')
return parser.parse_args()
def main():
args = get_args()
try:
file = open(args.filename)
except FileNotFoundError as err:
print(f"{err}. No such file: '{args.filename}'. Please provide text user list, eg user.txt. Exiting")
sys.exit(2)
else:
print("---\n", "# Ansible vars file for users\n", "users:", sep="\n")
with file:
for lines in file:
''' Ignore/skip blank or incomplete lines that do not contain 7 fields with continue '''
# if len(lines.split(':')) < 7 or len(lines.split(':')) > 7:
# if not len(lines.split(':')) == 7:
if len(lines.split(':')) != 7:
continue
row = lines.strip()
row = row.split(':')
print(f" - {{ username: '{row[0]}' , comment: '{row[4]}' , uid: '{row[2]}' , group: '{row[3]}' , shell: '{row[6]}' }}")
if __name__ == '__main__':
main()