Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ansible/ansible.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSviatoslav Sydorenko <wk@sydorenko.org.ua>2020-07-02 23:13:33 +0300
committerSviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>2020-07-03 11:44:54 +0300
commit8d97c8c222d134cb1108310c5b22eb65ead2d2d3 (patch)
treec0cccd5148a5b9f0f88f93b6e03ea5fa2c143c65 /examples
parent0198ceebe40f9221909988def2c35405e6023924 (diff)
Fix the internal Python API usage examples
Previous version initialized the `TaskQueueManager` after calling `Play.load()` while advertising a way to inject a custom library location path. This caused the tasks loader not to find any custom modules because it was triggered before the path was actually added to the module loader. This patch changes the order of the operations to ensure that the customized `context.CLIARGS` actually influences things. Resolves https://github.com/ansible/ansible/issues/69758.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/scripts/uptime.py26
1 files changed, 17 insertions, 9 deletions
diff --git a/examples/scripts/uptime.py b/examples/scripts/uptime.py
index c403dd91019..6542ff55b39 100755
--- a/examples/scripts/uptime.py
+++ b/examples/scripts/uptime.py
@@ -48,10 +48,27 @@ def main():
loader = DataLoader()
passwords = dict()
+ # Instantiate our ResultsCollector for handling results as
+ # they come in. Ansible expects this to be one of its main
+ # display outlets.
+ callback = ResultsCollector()
+
# create inventory and pass to var manager
inventory = InventoryManager(loader=loader, sources=sources)
variable_manager = VariableManager(loader=loader, inventory=inventory)
+ # Instantiate task queue manager, which takes care of forking
+ # and setting up all objects to iterate over host list and tasks.
+ # IMPORTANT: This also adds library dirs paths to the module loader
+ # IMPORTANT: and so it must be initialized before calling `Play.load()`.
+ tqm = TaskQueueManager(
+ inventory=inventory,
+ variable_manager=variable_manager,
+ loader=loader,
+ passwords=passwords,
+ stdout_callback=callback,
+ )
+
# create play with tasks
play_source = dict(
name="Ansible Play",
@@ -62,16 +79,7 @@ def main():
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
# actually run it
- tqm = None
- callback = ResultsCollector()
try:
- tqm = TaskQueueManager(
- inventory=inventory,
- variable_manager=variable_manager,
- loader=loader,
- passwords=passwords,
- stdout_callback=callback,
- )
result = tqm.run(play)
finally:
if tqm is not None: