How to call ansible-playbook from custom python code

mkyrc
Thu, 09 May 2024 12:20:28 +0000
Get Help

Hello all,
I’d like to ask you about preferred method how to call playbook from custom python code.

We have our own Python based orchestration tool where we can call (not only) ansible-playbooks (we are using direct shell command call, not ansible api) with some settings (playbook path, extra variables, ansible settings, etc).

Everything is working, but we are getting playbook/tasks statistics from text output starting RECAP line - I know, not very nice method, but for this older custom project that was only one way how to do it.

Now we’d like to change it and that’s the reason to ask you about best way how to run playbook from Python code more ‘programmatically’.

I know, that using Ansible Python API is not recommended method because it can changed in future. Therefore I was playing with ansible-runner project but there are several limitations for us:

ansible_runner.interface.run()

cons:

can’t correctly send ‘ctrl+c’ to ansible playbook

it’s using strict directory hierarchy

there are some issues with vault

pros:

we can get stats as dictionary (success, changed, errors,…)

ansible_runner.interface.run_command()

cons:

no stats (only out (str), err (str) and rc (int)

pros:

simple

this can be helpful for us, but we preferred stats as dictionary (similar to run())

My question is:

Can we use Ansible Python API directly?

I know that ansible-runner project is recommended way, but there are some limitations for us

Is there any other method how to call ansible playbook and get run results?

Thank you for the recommendations how to solve this question(s)

6 posts - 6 participants