Playbook into python

Hi all,

I’m newer in ansible, so I made some playbooks (working fine). But now I want to call these playbooks inside python script and record the output from ansible playbook in python variable. How can i do that?
See thats playbook works:

[root@vmcaiosyscolprod01 playbooks]# ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.5/site-packages/ansible-2.4.2.0-py3.5.egg/ansible
executable location = /usr/local/bin/ansible
python version = 3.5.3 (default, Apr 17 2017, 12:33:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@vmcaiosyscolprod01 playbooks]#

[root@vmcaiosyscolprod01 playbooks]# /usr/bin/ansible-playbook QOS_S1.yml -i S1_QOS_inventory.yml

PLAY [GET STRUCTURED DATA BACK FROM CLI DEVICES] *******************************************************************************************************

TASK [GET INTERFACES in S1 VRF] ************************************************************************************************************************
ok: [RJJAC77-RMP01]

TASK [debug] *******************************************************************************************************************************************
ok: [RJJAC77-RMP01] => {
“result”: {
“changed”: false,
“failed”: false,
“response”: [
{
“address”: “10.205.12.162”,
“dead_time”: “00:00:36”,
“interface”: “GigabitEthernet0/24”,
“neighbor_id”: “10.205.12.132”,
“priority”: “0”,
“state”: “FULL/ -”
},
{
“address”: “10.205.12.153”,
“dead_time”: “00:00:38”,
“interface”: “Vlan171”,
“neighbor_id”: “10.205.12.130”,
“priority”: “0”,
“state”: “FULL/ -”
}
],
“response_list”:
}
}

PLAY RECAP *********************************************************************************************************************************************
RJJAC77-RMP01 : ok=2 changed=0 unreachable=0 failed=0

[root@vmcaiosyscolprod01 playbooks]#

In Internet I found some directions see;

from ansible.playbook import Playbook
from ansible import inventory
from ansible.plugins import callback
from ansible import utils
import jinja2
from tempfile import NamedTemporaryFile
import os

#callbacks for stdout/stderr and log output
utils.VERBOSITY = 0
playbook_cb = callback.PlaybookCallbacks(verbose=utils.VERBOSITY)
stats = callbacks.AggregateStats()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

pb = PlayBook(
playbook=‘/usr/local/caiosys/ansible/playbooks/QOS_S1.yml’,
host_list=‘/usr/local/caiosys/ansible/playbooks/S1_QOS_inventory.yml’,
become=True,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
stats=stats,
)

results = pb.run()

Ensure on_stats callback is called for callback modules

playbook_cb.on_stats(pb.stats)

print results

But I fount some isues see:

[root@vmcaiosyscolprod01 playbooks]# python3
Python 3.5.3 (default, Apr 17 2017, 12:33:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

from ansible.playbook import Playbook
from ansible import inventory
from ansible.plugins import callback
from ansible import utils
import jinja2
from tempfile import NamedTemporaryFile
import os
utils.VERBOSITY = 0
playbook_cb = callback.PlaybookCallbacks(verbose=utils.VERBOSITY)
Traceback (most recent call last):
File “”, line 1, in
AttributeError: module ‘ansible.plugins.callback’ has no attribute ‘PlaybookCallbacks’

Where is PlaybookCallbacks???

Thanks
Fábio