(Python API question) Recursively expanding Ansible host vars from a standalone Python script

Hi folks - In a standalone Python script, I need to enumerate and expand all host variables from a given Ansible inventory file. In this inventory file, there are variables which reference other variables using Jinja2 templates, and the level of recursion in those can be 3-4 levels deep. Some of the vars use Jinja2 tests (such as the version (version_compare) test) within their templates.

After some experimentation I could combine Ansible libraries with Jinja2 to do this. But it looks like a lot of work. Is there an intrinsic way to use ansible Python API and recursively expand all host variables?

FYI, here is what currently works for me:

from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
from ansible.plugins.test.core import version_compare
from jinja2 import Template, Environment

sources = [‘/project/ansible/conf/hosts’]
loader = DataLoader()
inventory = InventoryManager(loader=loader, sources=sources)
variable_manager = VariableManager(loader=loader, inventory=inventory)

myhost = inventory.get_hosts()[0]
vars = variable_manager.get_vars(host = myhost)

environment = Environment()
environment.tests[“version”] = version_compare

for k, v in vars.items():
if type(v) == str:
expanded_value = environment.from_string(v)

while True:
expanded_value = expanded_value.render(vars)
if “{{” not in expanded_value:
break
else:
expanded_value = environment.from_string(expanded_value)

print(k, “:”, expanded_value)

Since you are already importing things from ansible, I’d recommend you import Templar, and use that instead of directly using jinja2. Templar already has this functionality.

I’m interested in this as well, if possible please post any results

Thank you for the suggestion re: Templar. I was able to accomplish the needed functionality with this code:

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

sources = [‘/project/ansible/conf/hosts’]
loader = DataLoader()
inventory = InventoryManager(loader=loader, sources=sources)
variable_manager = VariableManager(loader=loader, inventory=inventory)

myhost = inventory.get_hosts()[0]
vars = variable_manager.get_vars(host = myhost)

templar = Templar(loader= loader, variables=vars)

for k, v in vars.items():
if type(v) == str:
expanded_value = templar.template(v)
print(k, “:”, expanded_value)

Hi there,

I have a very similar problem using ansible-inventory which does not perform this expansion either and this is causing me issues with AWX (see https://groups.google.com/g/awx-project/c/uW2jyvqA9MY for more information).

Your code opens up new possibilities for me as I could use it as a base to write a custom inventory plugin which I think could perform this expansion using the extra variables specified via --extra-vars (my inventory variables reference those extra vars via Jinja templates and do not currently get expanded).

Thanks a lot for sharing !

Vincent