I have a big ansible project where I have separated my files into 
playbooks, vars files, an inventory folder and roles. These files make 
extensive use of jinja templates.

For my use case, I need to be able to access the variables in these files 
outside ansible. So far, I have included `copy` tasks to dump the variable 
I need to templated out vars files. Then, in my app I read those with the 
python `yaml` module without issue since they are free of jinja templates. 
The variables I am interested in are the variables in the `vars` section of 
a playbook as well as those in `vars_files`. Such variables, get merged 
with role defaults.

I am now trying to remove those `copy` tasks and instead use the Ansible 
API to get the final value for the variables. This will also have the 
benefit of fully respecting the precedence rules baked into Ansible. So 
far, this is what I have come up with.

```
#!/usr/bin/env python 

import code
import os
import readline 
import rlcompleter 

os.environ["ANSIBLE_CONFIG"] = "ansible/ansible.cfg" 
 
import ansible.constants as C 
from ansible.inventory.manager
import InventoryManager
from ansible.parsing.dataloader import DataLoader 
from ansible.vars.manager import VariableManager 
from ansible.template import Templar

from ansible.playbook import Playbook 

loader = DataLoader() 
inventory = InventoryManager( loader=loader, 
sources=["data/worlds/active/inventory/configs"] ) 
variable_manager = VariableManager(loader=loader, inventory=inventory) 

p = Playbook.load( "ansible/plays/instances/main.yml", loader=loader, 
variable_manager=variable_manager) 

readline.parse_and_bind("tab: complete") 
code.InteractiveConsole(locals=globals()).interact() 
```

I don't know how to proceed. I am supposed to actually run the tasks in the 
playbook to manage to get the variables? Can I somehow achieve what I want 
with the Ansible API without running any task? I don't care about 
`set_fact` or dynamic creation/alteration of the variables.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/130c4ab9-028f-4c20-8802-04a8691dcc2fn%40googlegroups.com.

Reply via email to