One way to achieving this is using the ansible API which all information is listed here:
https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
You can also find a library that will do this for you.
As an alternative, you can use subprocess, a native library, and run ansible as any other command:
import subprocess
cmd = ["ansible-playbook", "-i", "inventory.ini", "playbook.yml"]
process = subprocess.Popen(cmd,stdout=subprocess.PIPE)
while True:
output = str(process.stdout.readline(), encoding="utf-8")
if process.poll() is not None:
break
else:
print(output)
In this case the idea is to print line by line from python. But it can be modified to any other application.
Here some references:
https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python
https://github.com/uehara1414/ansible-subprocess/blob/master/ansible_subprocess/main.py