Skip to content

Commit a029e52

Browse files
committed
Suppresses Ansible warning when creating multiple Api instances
1 parent cab055d commit a029e52

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
---------
33

4+
- Avoids Ansible spamming us with warnings when creating multiple
5+
`Api` instances.
6+
[Daverball]
7+
48
0.20.2 (2024-09-03)
59
~~~~~~~~~~~~~~~~~~~
610

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ log_level = "INFO"
77
testpaths = ["tests"]
88
filterwarnings = [
99
"ignore:The _yaml extension module is now located at yaml._yaml",
10-
"ignore:AnsibleCollectionFinder has already been configured"
1110
]
1211

1312
[tool.coverage.run]

src/suitable/api.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def __init__(
136136
Provide a custom path or sequence of path to look for ansible
137137
collections when loading/hooking the modules.
138138
139+
Ansible only initializes the module loader once, so it's not
140+
possible to have multiple `Api` instances with different
141+
values for this parameter. The first one will always be the
142+
one that matters.
143+
144+
Additionally if the loader has already been initialized prior
145+
to the creation of the Api instance, then this parameter has
146+
no effect at all.
147+
139148
Requires ansible-core >= 2.15
140149
141150
:param host_key_checking:
@@ -237,8 +246,19 @@ def __init__(
237246
collections_path = [collections_path]
238247

239248
if Version(__version__) >= Version('2.15'):
249+
import warnings
240250
from ansible.plugins.loader import init_plugin_loader
241-
init_plugin_loader(collections_path)
251+
# NOTE: Ansible emits a warning here, but it's harmless to
252+
# call this function multiple times, it just doesn't
253+
# do anything the second time around.
254+
# We don't want to propagate this warning.
255+
with warnings.catch_warnings():
256+
warnings.filterwarnings(
257+
'ignore',
258+
r'AnsibleCollectionFinder has already been configured',
259+
UserWarning
260+
)
261+
init_plugin_loader(collections_path)
242262

243263
for module in list_ansible_modules():
244264
ModuleRunner(module).hookup(self)

0 commit comments

Comments
 (0)