# Example for running a playbook using Python API

**URL:** <https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673>\
**Category:** Ansible Project\
**Created:** [November 3, 2014, 6:36pm UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673 "2014-11-03T18:36:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Fleming](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@Brian\_Fleming](https://forum.ansible.com/u/Brian_Fleming)\
**Post date:** [November 3, 2014, 6:36pm UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673/1 "2014-11-03T18:36:34Z")

</div>

`

import ansible.runner  
import ansible.playbook  
from ansible import callbacks  
from ansible import utils

stats = callbacks.AggregateStats()  
playbook\_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)  
runner\_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

pb = ansible.playbook.PlayBook(  
playbook=“nseries.yml”,  
stats=stats,  
callbacks=playbook\_cb,  
runner\_callbacks=runner\_cb,  
check=True  
)  
for (play\_ds, play\_basedir) in zip(pb.playbook, pb.play\_basedirs):  
import ipdb  
ipdb.set\_trace()

# Can play around here to see what’s going on.

pb.run() # This runs the playbook

`

---

<div class="post-metadata">

**Author:** ![vervas.g](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@vervas.g](https://forum.ansible.com/u/vervas.g)\
**Post date:** [February 23, 2015, 2:11pm UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673/2 "2015-02-23T14:11:54Z")

</div>

Hi Jazzed,

I used your example as a base for triggering ansible through a python script, however while the output suggest that the connection to the node is made and the playbook is running, individual steps on the various roles fail consistently in contrast to running the playbook using the ansible-playbook command line tool passing the same playbook.

The output suggests no clear reasons for this behavior.

Any ideas on that?

Thanks,  
Dimitris

---

<div class="post-metadata">

**Author:** ![Lavish\_Aggarwal](https://avatars.discourse-cdn.com/v4/letter/l/4af34b/32.png) [@Lavish\_Aggarwal](https://forum.ansible.com/u/Lavish_Aggarwal)\
**Post date:** [February 28, 2015, 7:02am UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673/3 "2015-02-28T07:02:00Z")

</div>

Thanks Jazzed,

Its working fine only if I have my host listed in /etc/ansible/hosts, but I want to pass my custom host file while running a playbook. Can you tell me how to do so?

---

<div class="post-metadata">

**Author:** ![Mike\_Putnam](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Mike\_Putnam](https://forum.ansible.com/u/Mike_Putnam)\
**Post date:** [March 16, 2015, 12:46am UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673/4 "2015-03-16T00:46:50Z")

</div>

Lavish,

You can override the default inventory file ( /etc/ansible/hosts ) with the shell environment variable ANSIBLE\_HOSTS.

I use the EC2 dynamic inventory script ( [http://docs.ansible.com/intro\_dynamic\_inventory.html#example-aws-ec2-external-inventory-script](http://docs.ansible.com/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script) ) like this:

$ ANSIBLE\_HOSTS=/path/to/ec2.py python myscript.py

---

<div class="post-metadata">

**Author:** ![user117](https://avatars.discourse-cdn.com/v4/letter/u/258eb7/32.png) [@user117](https://forum.ansible.com/u/user117)\
**Post date:** [February 22, 2017, 8:47am UTC](https://forum.ansible.com/t/example-for-running-a-playbook-using-python-api/16673/5 "2017-02-22T08:47:54Z")

</div>

How to run ansible-playbook on ansible2.0 or later version?  
The ansible.playbook has been removed.

在 2014年11月4日星期二 UTC+8上午2:36:35，Jazzed写道：
