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

Single string not cast to list of strings in role argument_spec validation #83268

Closed
1 task done
egdoc opened this issue May 18, 2024 · 4 comments
Closed
1 task done
Labels
affects_2.16 bug This issue/PR relates to a bug.

Comments

@egdoc
Copy link

egdoc commented May 18, 2024

Summary

During module parameters validation, if a value must be a list and a single string is provided, the value is turned into a list with that string as the only element, due to this code:

def check_type_list(value):
"""Verify that the value is a list or convert to a list
A comma separated string will be split into a list. Raises a :class:`TypeError`
if unable to convert to a list.
:arg value: Value to validate or convert to a list
:returns: Original value if it is already a list, single item list if a
float, int, or string without commas, or a multi-item list if a
comma-delimited string.
"""
if isinstance(value, list):
return value
if isinstance(value, string_types):
return value.split(",")
elif isinstance(value, int) or isinstance(value, float):
return [str(value)]
raise TypeError('%s cannot be converted to a list' % type(value))

Although the validation passes, the function doesn't seem to be applied when validating a role.

Issue Type

Bug Report

Component Name

Role Argument Spec Validation

Ansible Version

ansible [core 2.16.6]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/doc/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.12/site-packages/ansible
  ansible collection location = /home/doc/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True

Configuration

CONFIG_FILE() = /etc/ansible/ansible.cfg
EDITOR(env: EDITOR) = /usr/bin/vim

OS / Environment

Fedora 40

Steps to Reproduce

meta/argument_specs.py

---
argument_specs:
  main:
    short_description: test_role description.
    options:
      test_option:
        type: list
        elements: str
        required: true

tasks/main.yml

---
- name: Print var
  ansible.builtin.debug:
    msg: '{{ test_option }}'

test_playbook.yml

---
- name: Test
  hosts: localhost
  roles:
    - role: test_role
      test_option: singlestring

Playbook run:

ansible-playbook test_playbook.yml

PLAY [Test] **************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************
ok: [localhost]

TASK [test_role : Validating arguments against arg spec 'main' - test_role description.] *******************************************************************
ok: [localhost]

Expected Results

TASK [test_role: Print var] ******************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [ 
              "singlestring",
    ]
}

Actual Results

TASK [test_role: Print var] ******************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "singlestring"
}

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 May 18, 2024
@ansibot
Copy link
Contributor

ansibot commented May 18, 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.

@flowerysong
Copy link
Contributor

Role argument validation is just validation and cannot change the content of variables, This is unlikely to change since it would introduce even more complexity to the already complex variable model, so default is just for documentation and the existing type validators only guarantee that it can be converted to the type, not that it is actually that type. See #81575 for the planned addition of strict validators, which is evidently on hold until data tagging lands.

@sivel
Copy link
Member

sivel commented May 20, 2024

Duplicate of #78889

@sivel sivel marked this as a duplicate of #78889 May 20, 2024
@egdoc
Copy link
Author

egdoc commented May 20, 2024

Got it, thanks.

@egdoc egdoc closed this as completed May 20, 2024
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.16 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

4 participants