Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. mtayseer revised this gist Jan 16, 2015. 1 changed file with 17 additions and 5 deletions.
    22 changes: 17 additions & 5 deletions Ansible module to list groups in inventory (Python version :D)
    Original 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 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, re.MULTILINE)[0]
    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)
    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) + '}'
  2. mtayseer revised this gist Jan 16, 2015. 1 changed file with 1 addition and 1 deletion.
    Original 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)[0]
    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()
  3. mtayseer revised this gist Jan 16, 2015. 1 changed file with 11 additions and 25 deletions.
    36 changes: 11 additions & 25 deletions Ansible module to list groups in inventory (Python version :D)
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,20 @@
    #! /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
    # 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.
    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()

    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.
    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

    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()) + '}'
    print '{"Inventory Groups": ' + json.dumps(groups_list) + '}'
  4. @aabouzaid aabouzaid revised this gist Jan 16, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Ansible module to list groups in inventory (Python version :D)
    Original 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.
    #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]
    for line in ansible_conf_file:
    if regexp.search(line):
    return re.sub(r"(\n|(?<==)\s+)", "", line).split("=")[1]

    hosts_file = get_hosts_file("/etc/ansible/ansible.cfg")
    hosts_file = get_hosts_file("ansible.cfg")
    cat_hosts_file = open(hosts_file).readlines()

    #Get groups from inventory file and add it to array.
  5. @aabouzaid aabouzaid created this gist Jan 16, 2015.
    34 changes: 34 additions & 0 deletions Ansible module to list groups in inventory (Python version :D)
    Original 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()) + '}'