# Run Ansible playbook progragmatically?

**URL:** <https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631>\
**Category:** Ansible Project\
**Created:** [May 25, 2018, 4:56pm UTC](https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631 "2018-05-25T16:56:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_O\_Hara](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@Richard\_O\_Hara](https://forum.ansible.com/u/Richard_O_Hara)\
**Post date:** [May 25, 2018, 4:56pm UTC](https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631/1 "2018-05-25T16:56:06Z")

</div>

I have a python application that calls the code below with the plan to run an Ansible playbook programmatically via the Ansible API versus using something like subprocess.

The code below runs but nothing actually seems to get executed. Grabbing the output of results just gives me a dictionary that looks like:

`  
[{‘plays’: [localhost], ‘playbook’: ‘playbooks/asg\_elb\_example.yml’}]

`

I am not sure where I am going wrong or what I am missing. Here is the code I am running.

`  
import os  
import sys  
SCRIPT\_DIR = os.path.dirname(os.path.abspath( **file** ))  
ROOT\_DIR = os.path.dirname(SCRIPT\_DIR)

import json  
from collections import namedtuple  
from ansible.parsing.dataloader import DataLoader  
from ansible.vars.manager import VariableManager  
from ansible.inventory.manager import InventoryManager  
from ansible.playbook.play import Play  
from ansible.executor.playbook\_executor import PlaybookExecutor

def ansible\_part():  
playbook\_path = “playbooks/asg\_elb\_example.yml”  
inventory\_path = “hosts”

Options = namedtuple(‘Options’, [‘connection’, ‘module\_path’, ‘forks’, ‘become’, ‘become\_method’, ‘become\_user’, ‘check’, ‘diff’, ‘listhosts’, ‘listtasks’, ‘listtags’, ‘syntax’])  
loader = DataLoader()  
options = Options(connection=‘local’, module\_path=‘%s/’ % (ROOT\_DIR), forks=100, become=None, become\_method=None, become\_user=None, check=False,  
diff=False, listhosts=True, listtasks=False, listtags=False, syntax=False)  
passwords = dict(vault\_pass=‘secret’)

inventory = InventoryManager(loader=loader, sources=[inventory\_path])  
variable\_manager = VariableManager(loader=loader, inventory=inventory)  
executor = PlaybookExecutor(  
playbooks=[playbook\_path], inventory=inventory, variable\_manager=variable\_manager, loader=loader,  
options=options, passwords=passwords)  
results = executor.run()  
print results

def main():  
ansible\_part()  
`

`sys.exit(main())`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 25, 2018, 5:56pm UTC](https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631/2 "2018-05-25T17:56:50Z")

</div>

We do not recommend interfacing directly with the ansible playbook python API. It is not a public interface and is not guaranteed to be stable.

Instead, I’d recommend looking at the ansible-runner project, which provides a python API for executing playbooks: [https://github.com/ansible/ansible-runner](https://github.com/ansible/ansible-runner)

---

<div class="post-metadata">

**Author:** ![Richard\_O\_Hara](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@Richard\_O\_Hara](https://forum.ansible.com/u/Richard_O_Hara)\
**Post date:** [May 26, 2018, 6:34am UTC](https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631/3 "2018-05-26T06:34:45Z")

</div>

I have installed ansible and ansible-runn

I tried the following, just something simple

`import ansible_runner r =ansible_runner.run(playbook="asg_elb_example.yml",private_data_dir="demo")`

However ansible-runner is complaining about:  
**pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: ansible-playbook.**

I am not sure what the issue is since I can run ansible-playbook from the command line with no issues.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 26, 2018, 12:06pm UTC](https://forum.ansible.com/t/run-ansible-playbook-progragmatically/27631/4 "2018-05-26T12:06:08Z")

</div>

listhosts is true, any list option bypasses execution.
