Created
December 1, 2017 19:07
-
-
Save cidrblock/0967b2670f06ca377b854a21909c2119 to your computer and use it in GitHub Desktop.
Dump the inventory for a device
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 characters
#!/usr/bin/env python | |
import argparse | |
from ansible.parsing.dataloader import DataLoader | |
from ansible.vars.manager import VariableManager | |
from ansible.inventory.manager import InventoryManager | |
from cbkutils.utils import ppc | |
def collect(args): | |
""" Collect vars for a device | |
""" | |
loader = DataLoader() | |
inventory = InventoryManager(loader=loader, sources=[args.inventory_source]) | |
host = inventory.get_host(args.device) | |
variable_manager = VariableManager(loader=loader, inventory=inventory) | |
magic_vars = ['ansible_playbook_python', 'groups', 'group_names', 'inventory_dir', | |
'inventory_file', 'inventory_hostname', 'inventory_hostname_short', | |
'omit', 'playbook_dir'] | |
all_vars = variable_manager.get_vars(host=host, include_hostvars=True) | |
cleaned = ({k: v for (k, v) in all_vars.items() if k not in magic_vars}) | |
return cleaned | |
def main(): | |
""" The main entry point | |
""" | |
parser = argparse.ArgumentParser(description='inventory dumper') | |
parser.add_argument('-i', '--inventory', action="store", dest="inventory_source", | |
required=True, help="The location of the inventory") | |
parser.add_argument('-d', '--device', action="store", dest="device", | |
required=True, help="The host to dump.") | |
args = parser.parse_args() | |
ppc(collect(args)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment