Skip to content

Instantly share code, notes, and snippets.

@martbhell
Created July 5, 2016 19:19
Show Gist options
  • Save martbhell/7266d7b1e4e1be1ebbd3ed5d6fe3216f to your computer and use it in GitHub Desktop.
Save martbhell/7266d7b1e4e1be1ebbd3ed5d6fe3216f to your computer and use it in GitHub Desktop.
An inventory script that can show groups of groups
#!/usr/bin/python
import argparse
# for parsing the existing inventory
from ansible.inventory import Inventory
from ansible.parsing.dataloader import DataLoader
from ansible.vars import VariableManager
###args
parser = argparse.ArgumentParser(description='Different inventory script')
parser.add_argument('--list', dest='list', action='store_true')
parser.add_argument('--host', dest='host', action='store')
args = parser.parse_args()
host = args.host
llist = args.list
def hostfunction(host):
print host
print "Host"
def listfunction(llist):
variable_manager = VariableManager()
loader = DataLoader()
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='hosts')
groups = inventory.groups
for group in groups:
dogroup = inventory.get_group(group)
if dogroup.child_groups:
print "child groups of %s is %s" %(group,dogroup.child_groups)
#########
if args.host:
hostfunction(args.host)
elif args.list:
listfunction(args.list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment