Forked from aabouzaid/Ansible module to list groups in inventory (Python version :D)
Last active
December 14, 2016 00:18
Revisions
-
mtayseer revised this gist
Jan 16, 2015 . 1 changed file with 17 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,18 +3,30 @@ # You can print output like "pretty-print" outside Ansible by using: #./listgroups /path/to/ansible.cfg | python -m json.tool import sys import re import json #get hosts inventory from ansible.cfg file. ansible_conf_file = open(sys.argv[1]).read() hosts_file = re.findall('hostfile\s*=\s*(.*)', ansible_conf_file)[0] #Get groups from inventory file and add it to array. cat_hosts_file = open(hosts_file).readlines() group = 'Default' # for hosts without a group groups_list = {group: 0} for line in cat_hosts_file: # Skip comments & empty lines line = line.strip() if not line or line.startswith('#'): continue if line.startswith('['): # group group = re.sub(r'[\[\]]', '', line) groups_list[group] = 0 else: # host groups_list[group] += 1 #Print output in json format. print '{"Inventory Groups": ' + json.dumps(groups_list) + '}' -
mtayseer revised this gist
Jan 16, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import json #get hosts inventory from ansible.cfg file. ansible_conf_file = open(sys.argv[1]).read() hosts_file = re.findall('^hostfile\s*=\s*(.*)', ansible_conf_file, re.MULTILINE)[0] #Get groups from inventory file and add it to array. cat_hosts_file = open(hosts_file).read() -
mtayseer revised this gist
Jan 16, 2015 . 1 changed file with 11 additions and 25 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,20 @@ #! /bin/python # 7 minutes Ansible module to list groups in inventory (Python version) :D # You can print output like "pretty-print" outside Ansible by using: #./listgroups /path/to/ansible.cfg | python -m json.tool import os import sys import re import json #get hosts inventory from ansible.cfg file. ansible_conf_file = open(sys.argv[1]).read() hosts_file = re.findall('hostfile\s*=\s*(.*)', ansible_conf_file)[0] #Get groups from inventory file and add it to array. cat_hosts_file = open(hosts_file).read() groups_list = re.findall(r'\[(.*)\]', cat_hosts_file) #Print output in json format. print '{"Inventory Groups": ' + json.dumps(groups_list) + '}' -
aabouzaid revised this gist
Jan 16, 2015 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,16 +7,16 @@ import os import re import json #get hosts inventory from ansible.cfg file. def get_hosts_file(ansible_cfg): ansible_conf_file = open(ansible_cfg).readlines() regexp = re.compile('^hostfile') for line in ansible_conf_file: if regexp.search(line): return re.sub(r"(\n|(?<==)\s+)", "", line).split("=")[1] hosts_file = get_hosts_file("ansible.cfg") cat_hosts_file = open(hosts_file).readlines() #Get groups from inventory file and add it to array. -
aabouzaid created this gist
Jan 16, 2015 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ #! /bin/python #30 minutes Ansible module to list groups in inventory (Python version) :D #You can print output like "pretty-print" outside Ansible by using: #./listgroups | python -m json.tool import os import re import json #Get hosts inventory from ansible.cfg file. def get_hosts_file(ansible_cfg): ansible_conf_file = open(ansible_cfg).readlines() regexp = re.compile('^hostfile') for lines in ansible_conf_file: if regexp.search(lines) is not None: return lines.replace("\n", "").split("= ")[1] hosts_file = get_hosts_file("/etc/ansible/ansible.cfg") cat_hosts_file = open(hosts_file).readlines() #Get groups from inventory file and add it to array. def groups_list(): regexp = re.compile('^\[') gropus_array= [] for line in cat_hosts_file: if regexp.search(line) is not None: matched_line = re.sub(r'[\n\[\]]', '', line) gropus_array.append(matched_line) return gropus_array #Print output in json format. print '{"Inventory Groups": ' + json.dumps(groups_list()) + '}'