Skip to content

Commit

Permalink
Update group.fact
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk authored Oct 16, 2023
1 parent d06eec8 commit 79c03ca
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions ansible_facts.d/group.fact
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
#!/usr/bin/env python3
# https://github.com/ifor2u/specbase

import json
import sys

file = open("/etc/group", "r")
def parse_group_file(file_path):
"""
Parse the Unix group file and return data as a dictionary.
:param file_path: Path to the group file.
:return: Dictionary containing group data.
"""
list_groups = {}
try:
with open(file_path, "r+") as file:
for line in file:
tokens = line.rstrip().split(":")
group = {"gid": tokens[2], "group-list": []}
if len(tokens) == 4 and tokens[3]:
group["group-list"] = tokens[3].split(",")
list_groups[tokens[0]] = group
except IOError as e:
print(f"Failed to open {file_path}: {e}", file=sys.stderr)
sys.exit(1)
except IndexError:
print(f"Unexpected file format in {file_path}", file=sys.stderr)
sys.exit(1)
return list_groups

list_groups = {}
while True:
line = file.readline()
if not line:
break
tokens = line.rstrip().split(":")
group = {"gid": tokens[2], "group-list": []}
if len(tokens) == 4:
for token in tokens[3].split(","):
if token == "":
break
group["group-list"].append(token)
list_groups[tokens[0]] = group
def main():
group_file_path = "/etc/group"
groups_data = parse_group_file(group_file_path)
print(json.dumps(groups_data))

print(json.dumps(list_groups))
if __name__ == "__main__":
main()

0 comments on commit 79c03ca

Please sign in to comment.