One way to achieving this is using the ansible API which all information is listed here:
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:
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: