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

List validations fails if elements='int' and value is not within choices #82776

Closed
1 task done
kazagz opened this issue Mar 6, 2024 · 1 comment · Fixed by #82783
Closed
1 task done

List validations fails if elements='int' and value is not within choices #82776

kazagz opened this issue Mar 6, 2024 · 1 comment · Fixed by #82783
Labels
affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR.

Comments

@kazagz
Copy link

kazagz commented Mar 6, 2024

Summary

When validating argument_spec with entry that is type='list', elements='int' and choices set to be list of ints, it fails to show proper validation error if value is not within choices. Just to be clear, validation works, just module crashes because can't format message. If value is within choices range, then there is no error.

Issue Type

Bug Report

Component Name

lib/ansible/module_utils/common/parameters.py

Ansible Version

$ ansible --version
ansible [core 2.16.3]
  config file = /***/ansible.cfg
  configured module search path = ['/home/***/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /***/.venv/lib64/python3.11/site-packages/ansible
  ansible collection location = /***
  executable location = /***/.venv/bin/ansible
  python version = 3.11.5 (main, Sep 22 2023, 15:34:29) [GCC 8.5.0 20210514 (Red Hat 8.5.0-20)] (/***/.venv/bin/python3.11)
  jinja version = 3.1.3
  libyaml = True

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all
COLLECTIONS_PATHS(/***/ansible.cfg) = ['***']
CONFIG_FILE() = /***/ansible.cfg
EDITOR(env: EDITOR) = vim

OS / Environment

RHEL8

Steps to Reproduce

import json

from ansible.module_utils import basic
from ansible.module_utils.common.text.converters import to_bytes


def run_module():
    module_args = dict(
        foo=dict(type="list", elements="int", choices=[1]),
    )
    module = basic.AnsibleModule(argument_spec=module_args, supports_check_mode=True)
    assert module is not None


def main():
    args = json.dumps({"ANSIBLE_MODULE_ARGS": {"foo": [2]}})
    basic._ANSIBLE_ARGS = to_bytes(args)
    run_module()


if __name__ == "__main__":
    main()

Expected Results

{"failed": true, "msg": "value of foo must be one or more of: 1. Got no match for: 2", "invocation": {"module_args": {"foo": [2]}}}

Actual Results

Traceback (most recent call last):
  File "/***/ansible_collections/***/plugins/module_utils/example.py", line 22, in <module>
    main()
  File "/***/ansible_collections/***/plugins/module_utils/example.py", line 18, in main
    run_module()
  File "/***/ansible_collections/***/plugins/module_utils/example.py", line 11, in run_module
    module = basic.AnsibleModule(argument_spec=module_args, supports_check_mode=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/***/.venv/lib64/python3.11/site-packages/ansible/module_utils/basic.py", line 488, in __init__
    self.validation_result = self.validator.validate(self.params)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/***/.venv/lib64/python3.11/site-packages/ansible/module_utils/common/arg_spec.py", line 301, in validate
    result = super(ModuleArgumentSpecValidator, self).validate(parameters)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/***/.venv/lib64/python3.11/site-packages/ansible/module_utils/common/arg_spec.py", line 241, in validate
    _validate_argument_values(self.argument_spec, result._validated_parameters, errors=result.errors)
  File "/***/.venv/lib64/python3.11/site-packages/ansible/module_utils/common/parameters.py", line 666, in _validate_argument_values
    diff_str = ", ".join(diff_list)
               ^^^^^^^^^^^^^^^^^^^^
TypeError: sequence item 0: expected str instance, int found

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. affects_2.16 labels Mar 6, 2024
@ansibot
Copy link
Contributor

ansibot commented Mar 6, 2024

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the component bot command.

Akasurde added a commit to Akasurde/ansible that referenced this issue Mar 7, 2024
* Handle error raised when argument validation with elements=int
  and value is not within choices

Fixes: ansible#82776

Signed-off-by: Abhijeet Kasurde <[email protected]>
@ansibot ansibot added the has_pr This issue has an associated PR. label Mar 7, 2024
@jborean93 jborean93 removed the needs_triage Needs a first human triage before being processed. label Mar 7, 2024
jborean93 pushed a commit that referenced this issue Mar 7, 2024
* Handle error raised when argument validation with elements=int
  and value is not within choices

Fixes: #82776

Signed-off-by: Abhijeet Kasurde <[email protected]>
bcoca pushed a commit to bcoca/ansible that referenced this issue Mar 19, 2024
* Handle error raised when argument validation with elements=int
  and value is not within choices

Fixes: ansible#82776

Signed-off-by: Abhijeet Kasurde <[email protected]>
@ansible ansible locked and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants