Wednesday, 19 September 2018

Querying ansible global group variables via (python)

I'm trying to query global group variables set in Ansible. I seem to be getting an empty dictionary and I'm not sure what else I can do. My code looks like this:

def __init__(self, inventory_path=None):
    self.loader = DataLoader()
    self.variable_manager = VariableManager()
    self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory_path)
    self.variable_manager.set_inventory(self.inventory)

when I then try to get group vars as below:

    inventory_asg_groups = filter(lambda g: 'asg' in g, self.inventory.groups)
    for group in inventory_asg_groups:
        print(self.inventory.get_group_vars(self.inventory.get_group(group)))

I get an empty dictionary:

{}

when I just do a:

    print(self.inventory.localhost.vars)

I get this:

{'ansible_python_interpreter': '/usr/local/opt/python/bin/python2.7', 'ansible_connection': 'local'}

I know the inventory is being loaded, since I list all the groups in the inventory. How do I get the variables listed in group_vars/all via the python ansible api?



from Querying ansible global group variables via (python)

No comments:

Post a Comment