# Call ansible playbook/tasks from python script.

**URL:** <https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391>\
**Category:** Ansible Project\
**Created:** [May 22, 2019, 1:35am UTC](https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391 "2019-05-22T01:35:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [May 22, 2019, 1:35am UTC](https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391/1 "2019-05-22T01:35:15Z")

</div>

I have python script and have to call ansible playbook and tasks in various phases of script execution. What is preferred way to call ansible playbook or task?

I see python API was suggested to use for internal purpose only.  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_api.html#python-api-example](https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html#python-api-example)

The other way is to use subprocess. Want to know if subprocess can be used reliably ?

Thanks

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [May 22, 2019, 6:11am UTC](https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391/2 "2019-05-22T06:11:31Z")

</div>

I too want to use the Python API. Since Ansible does not guarantee Python API compatibility with older versions, this can become a moving target.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 22, 2019, 9:10am UTC](https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391/3 "2019-05-22T09:10:15Z")

</div>

You also have Ansible Runner that's created for this purpose

"A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible."

[https://github.com/ansible/ansible-runner](https://github.com/ansible/ansible-runner)

---

<div class="post-metadata">

**Author:** ![Yz\_Xyz](https://avatars.discourse-cdn.com/v4/letter/y/a698b9/32.png) [@Yz\_Xyz](https://forum.ansible.com/u/Yz_Xyz)\
**Post date:** [May 22, 2019, 1:36pm UTC](https://forum.ansible.com/t/call-ansible-playbook-tasks-from-python-script/30391/4 "2019-05-22T13:36:30Z")

</div>

I prefer going python API way.Looks to me there can be stability issues with versions.  
for now I will go with subprocess so that I dont have to end up resolving version conflicts down the line.

Thanks for sharing your experience.
