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

Make sure collection is a list if a str is given #69081

Merged
merged 4 commits into from
Apr 28, 2020

Conversation

Shrews
Copy link
Contributor

@Shrews Shrews commented Apr 21, 2020

SUMMARY

Supplying the collection name as a string instead of a list would cause a traceback
when we attempted to append the 'ansible.legacy' collection to the list we expected.

Fixes #69054

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

collections

ADDITIONAL INFORMATION

test.yaml

---
- hosts: localhost
  gather_facts: no
  collections: foo
  tasks:
    - debug: msg="Test"

results in:

  File "/Users/shrews/Devel/github/Shrews/ansible/lib/ansible/playbook/collectionsearch.py", line 31, in _ensure_default_collection
    collection_list.append('ansible.legacy')
AttributeError: 'AnsibleUnicode' object has no attribute 'append'

collection_list = [collection_list]
# We allow collections to be either a string or a list, so make sure
# we have a list going forward.
if isinstance(collection_list, string_types):
Copy link
Member

Choose a reason for hiding this comment

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

Should we insert an elif not isinstance(collection_list, (None, list)): and fail? As it's written, someone could supply all sorts of other incorrect data types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. If I make it a dict, we get the same traceback about not being able to call append(). I'll make that change.

Copy link
Member

Choose a reason for hiding this comment

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

instead of custom code, why just not force validation? this would handle 'non lists' already correctly and since 'collections' is static, we don't have to worry about templating

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bcoca If we assume CollectionSearch is always mixed in with Base (seems like it is?), then we can call self.get_validated_value() early on. I'm unclear on if the mixin is a safe assumption going forward though.

Copy link
Member

Choose a reason for hiding this comment

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

i really want to redesign it so 'get_validated_value' is the normal 'getter' and that 'get_unvalidated' becomes the method for when we need 'direct' access.

@ansibot
Copy link
Contributor

ansibot commented Apr 24, 2020

@Shrews this PR contains the following merge commits:

Please rebase your branch to remove these commits.

click here for bot help

@ansibot ansibot added affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_triage Needs a first human triage before being processed. 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. traceback This issue/PR includes a traceback. labels Apr 24, 2020
Because we are doing work on modifying the collections value before
it is actually validated, we can validate it ourselves early to make
sure the user supplies either a string or list. Dicts are not valid.

The new validation allows us to simplify the _ensure_default_collection()
function. And since the field is now static, we no longer need to specify
a default for it, which also allows us to simplify the function. Since
the default is now removed, we can also remove the sanity/ignore.txt entry
for collectionsearch.py.

New unit tests are added (and the existing one modified) that allow us to
make sure that we throw a parser error if a user specifies something other
than a string or list for the collections value everywhere it can be specified.
The default is actually used, so restore it.
@Shrews Shrews removed merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Apr 27, 2020
@sivel sivel merged commit ff47d3f into ansible:devel Apr 28, 2020
@Shrews Shrews deleted the 69054-bug-fix branch April 28, 2020 15:50
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Apr 29, 2020
@ansible ansible locked and limited conversation to collaborators May 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. 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. support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

collections playbook keyword does not support templating
6 participants