Hello everybody,
I am writing a python script by Asible2.0 python api. My script work well when I just run a playbook by PlaybookCLI(). Currently, I want to add a callback with running the playbook, but the callback function is not work in PlaybookCLI(). Who know the reason? If I mistake to use the PlaybookCLI, how to change? Thanks for you.
My ansible version is 2.1. The below is my code and the myplaybook.yaml just do “/usr/bin/uptime” in target machines.
#!/usr/bin/env python
from ansible.cli.playbook import PlaybookCLI
from ansible.plugins.callback import CallbackBase
class ResultCallback(CallbackBase):
def v2_runner_on_ok(self, result, **kwargs):
print(“this is my test”)
host = result._host
print json.dumps({host.name: result._result}, indent=4)
results_callback = ResultCallback()
my_cli=PlaybookCLI([‘’, ‘/home/vagrant/myplaybook.yaml’, ‘-s’], callback=results_callback)
my_cli.parse()
results=my_cli.run()