New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add optional module_utils import support #73832
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
minor_changes: | ||
- module payload builder - module_utils imports in any nested block (eg, ``try``, ``if``) are treated as optional during | ||
module payload builds; this allows modules to implement runtime fallback behavior for module_utils that do not exist | ||
in older versions of Ansible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...odule_utils/collections/ansible_collections/testns/testcoll/plugins/module_utils/legit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
|
||
def importme(): | ||
return "successfully imported from testns.testcoll" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
test/integration/targets/module_utils/library/test_optional.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/python | ||
# Most of these names are only available via PluginLoader so pylint doesn't | ||
# know they exist | ||
# pylint: disable=no-name-in-module | ||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
# internal constants to keep pylint from griping about constant-valued conditionals | ||
_private_false = False | ||
_private_true = True | ||
|
||
# module_utils import statements nested below any block are considered optional "best-effort" for AnsiballZ to include. | ||
# test a number of different import shapes and nesting types to exercise this... | ||
|
||
# first, some nested imports that should succeed... | ||
try: | ||
from ansible.module_utils.urls import fetch_url as yep1 | ||
except ImportError: | ||
yep1 = None | ||
|
||
try: | ||
import ansible.module_utils.common.text.converters as yep2 | ||
except ImportError: | ||
yep2 = None | ||
|
||
try: | ||
# optional import from a legit collection | ||
from ansible_collections.testns.testcoll.plugins.module_utils.legit import importme as yep3 | ||
except ImportError: | ||
yep3 = None | ||
|
||
# and a bunch that should fail to be found, but not break the module_utils payload build in the process... | ||
try: | ||
from ansible.module_utils.bogus import fromnope1 | ||
except ImportError: | ||
fromnope1 = None | ||
|
||
if _private_false: | ||
from ansible.module_utils.alsobogus import fromnope2 | ||
else: | ||
fromnope2 = None | ||
|
||
try: | ||
import ansible.module_utils.verybogus | ||
nope1 = ansible.module_utils.verybogus | ||
except ImportError: | ||
nope1 = None | ||
|
||
# deepish nested with multiple block types- make sure the AST walker made it all the way down | ||
try: | ||
if _private_true: | ||
if _private_true: | ||
if _private_true: | ||
if _private_true: | ||
try: | ||
import ansible.module_utils.stillbogus as nope2 | ||
except ImportError: | ||
raise | ||
except ImportError: | ||
nope2 = None | ||
|
||
try: | ||
# optional import from a valid collection with an invalid package | ||
from ansible_collections.testns.testcoll.plugins.module_utils.bogus import collnope1 | ||
except ImportError: | ||
collnope1 = None | ||
|
||
try: | ||
# optional import from a bogus collection | ||
from ansible_collections.bogusns.boguscoll.plugins.module_utils.bogus import collnope2 | ||
except ImportError: | ||
collnope2 = None | ||
|
||
module = AnsibleModule(argument_spec={}) | ||
|
||
if not all([yep1, yep2, yep3]): | ||
module.fail_json(msg='one or more existing optional imports did not resolve') | ||
|
||
if any([fromnope1, fromnope2, nope1, nope2, collnope1, collnope2]): | ||
module.fail_json(msg='one or more missing optional imports resolved unexpectedly') | ||
|
||
module.exit_json(msg='all missing optional imports behaved as expected') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be reasonable to add the collection loader code to pylintrc?