Merge pull request #16346 from sivel/issue/16341-2
Rework the v2 API example to use a custom callback
This commit is contained in:
commit
a0e24ec579
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
|
||||
|
||||
import json
|
||||
from collections import namedtuple
|
||||
from ansible.parsing.dataloader import DataLoader
|
||||
from ansible.vars import VariableManager
|
||||
from ansible.inventory import Inventory
|
||||
from ansible.playbook.play import Play
|
||||
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'])
|
||||
# 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()
|
||||
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')
|
||||
# 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
|
||||
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,
|
||||
options=options,
|
||||
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)
|
||||
finally:
|
||||
if tqm is not None:
|
||||
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:
|
||||
|
||||
|
|
Loading…
Reference in a new issue