# Question on using the Ansible API

**URL:** <https://forum.ansible.com/t/question-on-using-the-ansible-api/33235>\
**Category:** Ansible Project\
**Created:** [June 23, 2020, 3:53pm UTC](https://forum.ansible.com/t/question-on-using-the-ansible-api/33235 "2020-06-23T15:53:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Kakouros](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Nick\_Kakouros](https://forum.ansible.com/u/Nick_Kakouros)\
**Post date:** [June 23, 2020, 3:53pm UTC](https://forum.ansible.com/t/question-on-using-the-ansible-api/33235/1 "2020-06-23T15:53:58Z")

</div>

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.

```auto

#!/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.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [June 23, 2020, 10:50pm UTC](https://forum.ansible.com/t/question-on-using-the-ansible-api/33235/2 "2020-06-23T22:50:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nick\_Kakouros](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Nick\_Kakouros](https://forum.ansible.com/u/Nick_Kakouros)\
**Post date:** [June 24, 2020, 10:26am UTC](https://forum.ansible.com/t/question-on-using-the-ansible-api/33235/3 "2020-06-24T10:26:56Z")

</div>

You mean you do this using ansible tasks, right? If not, could you post a code example?
