How to get all inventory groups variables in hierarchy via Python API?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to get all inventory groups variables in hierarchy via Python API?



I want to collect all inventory hosts groups variables in hierarchy data struct and send them to Consul to make them available in runtime.



Calling this method - https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L160 I got the error


inventory.get_vars()
Traceback (most recent call last):
File "<input>", line 1, in <module>
inventory.get_vars()
File "<>/.virtualenvs/ansible27/lib/python2.7/site-packages/ansible/inventory/manager.py", line 160, in get_vars
return self._inventory.get_vars(args, kwargs)
AttributeError: 'InventoryData' object has no attribute 'get_vars'



my script


import pprint
pp = pprint.PrettyPrinter(indent=4).pprint

from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager

loader = DataLoader()
inventory = InventoryManager(loader=loader, sources='inventories/itops-vms.yml')
variable_manager = VariableManager(loader=loader, inventory=inventory)

# shows groups as well
pp(inventory.groups)

# shows dict as well with content
pp(variable_manager.get_vars())

# creates an unhandled exception
inventory.get_vars()



How to do that right way?





Did you try the list_groups method? You can check out all the available methods here github.com/ansible/ansible/blob/…
– Steven
Aug 10 at 18:11


list_groups





I don't understand what you mean by "all inventory hosts groups variables". What if variables in different groups had different values? What if hosts had variables defined?
– techraf
Aug 10 at 18:39






I want to get something similar to $ ansible-inventory -i inventories/itops-vms.yml --graph --vars result in python in some data struct which I could walk/read.
– greggyNapalm
Aug 10 at 20:55


$ ansible-inventory -i inventories/itops-vms.yml --graph --vars




1 Answer
1



The error itself seems to be caused by a bug - the get_vars method of the inventory object calls get_vars method of the InventoryData object which is not implemented.


get_vars


get_vars


InventoryData



You need to specify the group, for example:


>>> inventory.groups['all'].get_vars()
u'my_var': u'value'



You can create a dictionary with that data:


g: inventory.groups[g].get_vars() for g in inventory.groups



The above gets only the variables defined in the inventory itself (which is what the question asks about). If you wanted to get a structure with variables from group_vars, host_vars, etc. (as you indicated in your comment I want to get something similar to $ ansible-inventory -i inventories/itops-vms.yml --graph --vars you'd need to collect the data from different sources, just like Ansible does.


$ ansible-inventory -i inventories/itops-vms.yml --graph --vars






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard