Skip to content

Commit

Permalink
Suppresses Ansible warning when creating multiple Api instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Daverball committed Sep 4, 2024
1 parent cab055d commit a029e52
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
---------

- Avoids Ansible spamming us with warnings when creating multiple
`Api` instances.
[Daverball]

0.20.2 (2024-09-03)
~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ log_level = "INFO"
testpaths = ["tests"]
filterwarnings = [
"ignore:The _yaml extension module is now located at yaml._yaml",
"ignore:AnsibleCollectionFinder has already been configured"
]

[tool.coverage.run]
Expand Down
22 changes: 21 additions & 1 deletion src/suitable/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def __init__(
Provide a custom path or sequence of path to look for ansible
collections when loading/hooking the modules.
Ansible only initializes the module loader once, so it's not
possible to have multiple `Api` instances with different
values for this parameter. The first one will always be the
one that matters.
Additionally if the loader has already been initialized prior
to the creation of the Api instance, then this parameter has
no effect at all.
Requires ansible-core >= 2.15
:param host_key_checking:
Expand Down Expand Up @@ -237,8 +246,19 @@ def __init__(
collections_path = [collections_path]

if Version(__version__) >= Version('2.15'):
import warnings
from ansible.plugins.loader import init_plugin_loader
init_plugin_loader(collections_path)
# NOTE: Ansible emits a warning here, but it's harmless to
# call this function multiple times, it just doesn't
# do anything the second time around.
# We don't want to propagate this warning.
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
r'AnsibleCollectionFinder has already been configured',
UserWarning
)
init_plugin_loader(collections_path)

for module in list_ansible_modules():
ModuleRunner(module).hookup(self)
Expand Down

0 comments on commit a029e52

Please sign in to comment.