avoid literal quoting in config lists (#74740)

* added tests
This commit is contained in:
Brian Coca 2021-05-19 16:14:52 -04:00 committed by GitHub
parent 888fea69e5
commit d2d1f01f9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- config, ensure 'quoted' lists from ini or env do not take the quotes literally as part of the list item.

View file

@ -92,7 +92,7 @@ def ensure_type(value, value_type, origin=None):
elif value_type == 'list':
if isinstance(value, string_types):
value = [x.strip() for x in value.split(',')]
value = [unquote(x.strip()) for x in value.split(',')]
elif not isinstance(value, Sequence):
errmsg = 'list'

View file

@ -0,0 +1,66 @@
# (c) 2021 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = """
name: types
author: Ansible Core Team
version_added: histerical
short_description: returns what you gave it
description:
- this is mostly a noop
options:
_terms:
description: stuff to pass through
valid:
description: does nothihng, just for testing values
type: list
ini:
- section: list_values
key: valid
mustunquote:
description: does nothihng, just for testing values
type: list
ini:
- section: list_values
key: mustunquote
notvalid:
description: does nothihng, just for testing values
type: list
ini:
- section: list_values
key: notvalid
totallynotvalid:
description: does nothihng, just for testing values
type: list
ini:
- section: list_values
key: totallynotvalid
"""
EXAMPLES = """
- name: like some other plugins, this is mostly useless
debug: msg={{ q('types', [1,2,3])}}
"""
RETURN = """
_list:
description: basically the same as you fed in
type: list
elements: raw
"""
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
self.set_options(var_options=variables, direct=kwargs)
return terms

View file

@ -21,3 +21,6 @@ ANSIBLE_CONFIG=inline_comment_ansible.cfg ansible-config dump --only-changed | g
# test the config option validation
ansible-playbook validation.yml "$@"
# test types from config (just lists for now)
ANSIBLE_CONFIG=type_munging.cfg ansible-playbook types.yml "$@"

View file

@ -0,0 +1,8 @@
[defaults]
nothing = here
[list_values]
valid = 1, 2, 3
mustunquote = '1', '2', '3'
notvalid = [1, 2, 3]
totallynotvalid = ['1', '2', '3']

View file

@ -0,0 +1,25 @@
- hosts: localhost
gather_facts: false
tasks:
- name: ensures we got the list we expected
block:
- name: initialize plugin
debug: msg={{ lookup('types', 'starting test') }}
- set_fact:
valid: '{{ lookup("config", "valid", plugin_type="lookup", plugin_name="types") }}'
mustunquote: '{{ lookup("config", "mustunquote", plugin_type="lookup", plugin_name="types") }}'
notvalid: '{{ lookup("config", "notvalid", plugin_type="lookup", plugin_name="types") }}'
totallynotvalid: '{{ lookup("config", "totallynotvalid", plugin_type="lookup", plugin_name="types") }}'
- assert:
that:
- 'valid|type_debug == "list"'
- 'mustunquote|type_debug == "list"'
- 'notvalid|type_debug == "list"'
- 'totallynotvalid|type_debug == "list"'
- valid[0]|int == 1
- mustunquote[0]|int == 1
- "notvalid[0] == '[1'"
# using 'and true' to avoid quote hell
- totallynotvalid[0] == "['1'" and True