Skip to content
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

Error if a module is found to shadow a reserved keyword #34649

Merged
merged 7 commits into from
Apr 10, 2018

Conversation

sivel
Copy link
Member

@sivel sivel commented Jan 9, 2018

SUMMARY

Error if a module is found to shadow a reserved keyword

This prevents the error:

ERROR! conflicting action statements: command, tags

and replaces it with

ERROR! Module "tags" shadows the name of a reserved keyword. Please rename or remove this module. Found at /Users/matt/projects/ansibledev/playbooks/tags/library/tags
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

lib/ansible/plugins/loader.py

ANSIBLE VERSION

ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 feature_pull_request needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:community This issue/PR relates to code supported by the Ansible community. test This PR relates to tests. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 9, 2018
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Jan 11, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 19, 2018
@maxamillion
Copy link
Contributor

Fix merge conflicts and then ship it! 👍

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 23, 2018
@sivel
Copy link
Member Author

sivel commented Jan 23, 2018

Rebased, but not sure if we can merge to 2.5. I'll check on that.

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jan 23, 2018
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 6, 2018
@ansibot ansibot added feature This issue/PR relates to a feature request. and removed feature_pull_request labels Mar 2, 2018
@sivel sivel added this to the 2.7.0 milestone Mar 2, 2018
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Mar 8, 2018
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Mar 8, 2018
@sivel
Copy link
Member Author

sivel commented Mar 9, 2018

I will need to look into the failure. This worked in the past but fails now. Need to ensure this is limited to modules, and doesn't trigger on other plugins.

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Mar 9, 2018
@alikins
Copy link
Contributor

alikins commented Mar 9, 2018

@sivel A variation on this idea would be to stop raising AnsibleError for this case in mod_args and wait until keyword validation to raise an error.

I have an old branch at devel...alikins:site_role_devel that did part of this, though for different reasons. The 'site_role_devel' branch was fixing errors that would happen if there were no (or not all) modules available when the playbooks are being parsed/loaded. For that case, mod_args.parse would always fail in the same place because there it wouldn't find a matching module for the task arg.

@alikins
Copy link
Contributor

alikins commented Mar 9, 2018

devel...alikins:site_role_devel was for the case of not finding a matching module at all, but I think a similar approach could work for possibly conflicting action/args as well. Since we don't really know all the valid modules until later (ie, that 'tags.json' isnt a valid module and does not conflict with 'tags' attribute)

@sivel
Copy link
Member Author

sivel commented Mar 9, 2018

I suppose that is possible. It depends on when we want this. If we refactor, we are waiting until 2.7. If this goes in now, it could be part of 2.5 potentially.

I'd be concerned though with doing this in _validate_attributes because that would cause us to potentially wait longer to see an error. I've never run into a problem where I didn't have the module I needed to execute the playbook. My personal feeling is that it shouldn't happen and should be an error.

(ie, that 'tags.json' isnt a valid module and does not conflict with 'tags' attribute)

Not sure I follow this. How would we know that? We don't restrict module file extensions? When would we be able to say that tags.json isn't a module? As far as I know, that can only be detected by trying to run it, and even then likely not in a way you might expect.

@alikins
Copy link
Contributor

alikins commented Mar 9, 2018

@sivel

I'd be concerned though with doing this in _validate_attributes because that would cause us to potentially wait longer to see an error. I've never run into a problem where I didn't have the module I needed to execute the playbook. My personal feeling is that it shouldn't happen and should be an error.

I think showing an error later is fine if it is a better error. To use an analogy to python, the current behavior feels like raising a SyntaxError when it should be raising a NameError.

Neither the no modules error or the conflicting action error should happen if the system configuration is correct. And currently not having any modules (or at least ping ) is a fatal error. The no modules due to misconfiguration is pretty common.

For the site_role_devel branch, the goal was to be able to have all modules provided by a role, so the error handling in mod args had to be defered to later until after roles were loaded.

Not sure I follow this. How would we know that? We don't restrict module file extensions? When would we be able to say that tags.json isn't a module? As far as I know, that can only be detected by trying to run it, and even then likely not in a way you might expect.

The main goal there is that we would avoid needing to look for modules at that point. mod_args.parse() happens very early and requires module_loader.find_plugin() to run early when it could be deferred to later after any library path changes have 'settled' (at least until dynamic includes start modifying it again).

@sivel
Copy link
Member Author

sivel commented Mar 9, 2018

I'm not going to go down that path now. I don't see a path to implement this that executes later without boiling the ocean right now.

I'm going to aim for the path of least resistance.

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Mar 17, 2018
from ansible.vars.reserved import is_reserved_name

plugin = self._find_plugin(name, mod_type=mod_type, ignore_deprecated=ignore_deprecated, check_aliases=check_aliases)
if plugin and self.package == 'ansible.modules' and is_reserved_name(name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is limited to the ansible.modules package, is it able to catch any issues that would not be detected by a sanity test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this PR is not to resolve a CI related problem or to resolve issues with module submissions. It is however designed to solve issues our users face when they may have a file that is picked up as a module, that shadows the name of a keyword. There are 2 example issues linked to this PR that showcase the issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part I was missing here is that user's modules are also in the ansible.modules package. I was under the incorrect impression that they were in a separate package.

@sivel sivel merged commit f1082af into ansible:devel Apr 10, 2018
ilicmilan pushed a commit to ilicmilan/ansible that referenced this pull request Nov 7, 2018
* Error if a module is found to shadow a reserved keyword

* Add test for shadowed module

* Bring in functools.wraps for the decorator

* Drop the decorator, make _find_plugin the real function, find_plugin now holds the shadow logic

* Swap order of functions for bottom to top execution order

* Only error for modules

* Add test for loading a lookup plugin that shadows a keyword
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 feature This issue/PR relates to a feature request. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants