# PlaybookExecutor example

**URL:** <https://forum.ansible.com/t/playbookexecutor-example/20076>\
**Category:** Ansible Developer\
**Created:** [December 26, 2015, 7:39pm UTC](https://forum.ansible.com/t/playbookexecutor-example/20076 "2015-12-26T19:39:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goran\_Mekic](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@Goran\_Mekic](https://forum.ansible.com/u/Goran_Mekic)\
**Post date:** [December 26, 2015, 7:39pm UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/1 "2015-12-26T19:39:13Z")

</div>

Hello,

I'm trying to figure out how to use PlaybookExecutor. In short, I'd like  
to run my playbook from the Python code using Ansible 2. I tried poking  
the code but I'm just not smart enough, so a code snippet would be more  
than welcome. Thank you!

Regards,  
meka

---

<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:** [December 28, 2015, 3:14pm UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/2 "2015-12-28T15:14:11Z")

</div>

I’d start with looking at how ansible uses it:

[https://github.com/ansible/ansible/blob/c489b271d152820ab11b73d11877f8805318cd7a/lib/ansible/cli/playbook.py#L147-L150](https://github.com/ansible/ansible/blob/c489b271d152820ab11b73d11877f8805318cd7a/lib/ansible/cli/playbook.py#L147-L150)

From there, you should be able to look at how the vars it requires are constructed from further above in the code.

As far as self.options is concerned, there is a good approach listed at [http://docs.ansible.com/ansible/developing\_api.html#python-api-2-0](http://docs.ansible.com/ansible/developing_api.html#python-api-2-0)

---

<div class="post-metadata">

**Author:** ![Goran\_Mekic](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@Goran\_Mekic](https://forum.ansible.com/u/Goran_Mekic)\
**Post date:** [December 28, 2015, 11:35pm UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/3 "2015-12-28T23:35:58Z")

</div>

This really helps. Thank you A LOT!!!

As Ansible uses multiprocess for forking, it's imposible to use with  
Celery. Is there any other recommendation for running playbooks? Or,  
maybe, does anyone knows how Tower did it? Thank you!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 29, 2015, 8:47pm UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/4 "2015-12-29T20:47:18Z")

</div>

IIRC Tower uses Celery also, they just execute the ansible-playbook CLI

---

<div class="post-metadata">

**Author:** ![Goran\_Mekic](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@Goran\_Mekic](https://forum.ansible.com/u/Goran_Mekic)\
**Post date:** [December 30, 2015, 12:42am UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/5 "2015-12-30T00:42:08Z")

</div>

For now, I'm using os.system but if anyone has any idea how to run  
playbook programmaticaly, I'd be glad to hear it. Anyway, thank you very  
much for your help!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 1, 2016, 5:48am UTC](https://forum.ansible.com/t/playbookexecutor-example/20076/6 "2016-01-01T05:48:40Z")

</div>

Example: [http://docs.ansible.com/ansible/developing\_api.html](http://docs.ansible.com/ansible/developing_api.html) , just a  
reminder that it is GPL code and that doing it this way has  
implications for distribution and/or services to 3rd parties.
