Question on using the Ansible API

30 views
Skip to first unread message

tterr...@gmail.com

unread,
Jun 23, 2020, 11:53:58 AM6/23/20
to Ansible Project
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.

Pshem Kowalczyk

unread,
Jun 23, 2020, 6:50:40 PM6/23/20
to ansible...@googlegroups.com
Hi,

I had a similar requirement, but decided to approach it a bit differently. In my case the playbook always had to be run first, so I simply collected all the variables I needed into one dictionary and then stored that dictionary in a file for further reuse by other components. 

kind regards
Pshem


--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/130c4ab9-028f-4c20-8802-04a8691dcc2fn%40googlegroups.com.

tterr...@gmail.com

unread,
Jun 24, 2020, 6:26:57 AM6/24/20
to Ansible Project
You mean you do this using ansible tasks, right? If not, could you post a code example?
Reply all
Reply to author
Forward
0 new messages