Call ansible playbook/tasks from python script.

28 views
Skip to first unread message

Yz Xyz

unread,
May 21, 2019, 9:35:15 PM5/21/19
to Ansible Project
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.

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

Thanks

Sudheer Satyanarayana

unread,
May 22, 2019, 2:11:50 AM5/22/19
to ansible...@googlegroups.com
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.

I have been using subprocess to execute Ansible playbooks. Of course it works reliably like executing any other command. Ansible is no different.

--
Sudheer Satyanarayana
System Administrator And Developer
Tech Chorus - https://www.techchorus.net

Kai Stian Olstad

unread,
May 22, 2019, 5:10:27 AM5/22/19
to ansible...@googlegroups.com
On 22.05.2019 08:11, Sudheer Satyanarayana wrote:
> On 22/05/19 7:05 AM, Yz Xyz wrote:
>> 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
>>
>> The other way is to use subprocess. Want to know if subprocess can be
>> used reliably ?
>>
> 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.
>
> I have been using subprocess to execute Ansible playbooks. Of course
> it works reliably like executing any other command. Ansible is no
> different.

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


--
Kai Stian Olstad

Yz Xyz

unread,
May 22, 2019, 9:36:30 AM5/22/19
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages