mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Rework the v2 API example to use a custom callback to better show how callbacks can be used for handling results
This commit is contained in:
parent
eab10b2402
commit
ab65150a9e
1 changed files with 21 additions and 7 deletions
|
@ -39,13 +39,29 @@ In 2.0 things get a bit more complicated to start, but you end up with much more
|
||||||
|
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import json
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from ansible.parsing.dataloader import DataLoader
|
from ansible.parsing.dataloader import DataLoader
|
||||||
from ansible.vars import VariableManager
|
from ansible.vars import VariableManager
|
||||||
from ansible.inventory import Inventory
|
from ansible.inventory import Inventory
|
||||||
from ansible.playbook.play import Play
|
from ansible.playbook.play import Play
|
||||||
from ansible.executor.task_queue_manager import TaskQueueManager
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
||||||
from ansible.plugins import callback_loader
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
|
class ResultCallback(CallbackBase):
|
||||||
|
"""A sample callback plugin used for performing an action as results come in
|
||||||
|
|
||||||
|
If you want to collect all results into a single object for processing at
|
||||||
|
the end of the execution, look into utilizing the ``json`` callback plugin
|
||||||
|
or writing your own custom callback plugin
|
||||||
|
"""
|
||||||
|
def v2_runner_on_ok(self, result, **kwargs):
|
||||||
|
"""Print a json representation of the result
|
||||||
|
|
||||||
|
This method could store the result in an instance attribute for retrieval later
|
||||||
|
"""
|
||||||
|
host = result._host
|
||||||
|
print json.dumps({host.name: result._result}, indent=4)
|
||||||
|
|
||||||
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
|
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
|
||||||
# initialize needed objects
|
# initialize needed objects
|
||||||
|
@ -53,8 +69,9 @@ In 2.0 things get a bit more complicated to start, but you end up with much more
|
||||||
loader = DataLoader()
|
loader = DataLoader()
|
||||||
options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False)
|
options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False)
|
||||||
passwords = dict(vault_pass='secret')
|
passwords = dict(vault_pass='secret')
|
||||||
# Use the JSON callback plugin to store results
|
|
||||||
results_callback = callback_loader.get('json')
|
# Instantiate our ResultCallback for handling results as they come in
|
||||||
|
results_callback = ResultCallback()
|
||||||
|
|
||||||
# create inventory and pass to var manager
|
# create inventory and pass to var manager
|
||||||
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost')
|
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost')
|
||||||
|
@ -81,16 +98,13 @@ In 2.0 things get a bit more complicated to start, but you end up with much more
|
||||||
loader=loader,
|
loader=loader,
|
||||||
options=options,
|
options=options,
|
||||||
passwords=passwords,
|
passwords=passwords,
|
||||||
stdout_callback=results_callback, # Use JSON callback plugin
|
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin
|
||||||
)
|
)
|
||||||
result = tqm.run(play)
|
result = tqm.run(play)
|
||||||
finally:
|
finally:
|
||||||
if tqm is not None:
|
if tqm is not None:
|
||||||
tqm.cleanup()
|
tqm.cleanup()
|
||||||
|
|
||||||
# Print stdout from the first play, first task, of localhost
|
|
||||||
print(results_callback.results[0]['tasks'][0]['hosts']['localhost']['stdout'])
|
|
||||||
|
|
||||||
|
|
||||||
.. _python_api_old:
|
.. _python_api_old:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue