Update the example for API use to reflect new context object (#50723)

This commit is contained in:
Matt Martz 2019-01-21 11:21:06 -06:00 committed by GitHub
parent 787aa522cf
commit ab0985a83a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,13 +29,14 @@ This example is a simple demonstration that shows how to minimally run a couple
import json import json
import shutil import shutil
from collections import namedtuple from ansible.module_utils.common.collections import ImmutableDict
from ansible.parsing.dataloader import DataLoader from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager from ansible.inventory.manager import InventoryManager
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.callback import CallbackBase from ansible.plugins.callback import CallbackBase
from ansible import context
import ansible.constants as C import ansible.constants as C
class ResultCallback(CallbackBase): class ResultCallback(CallbackBase):
@ -53,9 +54,9 @@ This example is a simple demonstration that shows how to minimally run a couple
host = result._host host = result._host
print(json.dumps({host.name: result._result}, indent=4)) print(json.dumps({host.name: result._result}, indent=4))
# since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object # since the API is constructed for CLI it expects certain options to always be set in the context object
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff']) context.CLIARGS = ImmutableDict(connection='local', module_path=['/to/mymodules'], forks=10, become=None,
options = Options(connection='local', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) become_method=None, become_user=None, check=False, diff=False)
# initialize needed objects # initialize needed objects
loader = DataLoader() # Takes care of finding and reading yaml, json and ini files loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
@ -92,7 +93,6 @@ This example is a simple demonstration that shows how to minimally run a couple
inventory=inventory, inventory=inventory,
variable_manager=variable_manager, variable_manager=variable_manager,
loader=loader, loader=loader,
options=options,
passwords=passwords, passwords=passwords,
stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
) )