Skip to content

Commit

Permalink
Validate plugin option type 'dict' / 'dictionary' (ansible#71928)
Browse files Browse the repository at this point in the history
* Validate option type 'dict' / 'dictionary'.

* Add changelog fragment.

* Change type of 'environment' to list.
  • Loading branch information
felixfontein authored Sep 29, 2020
1 parent a077bca commit 8893a24
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/71928-ensure_type-dict.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "plugin option validation - now the option type ``dict``/``dictionary`` is also validated by the config manager (https://github.com/ansible/ansible/pull/71928)."
6 changes: 5 additions & 1 deletion lib/ansible/config/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ansible.config.data import ConfigData
from ansible.errors import AnsibleOptionsError, AnsibleError
from ansible.module_utils._text import to_text, to_bytes, to_native
from ansible.module_utils.common._collections_compat import Sequence
from ansible.module_utils.common._collections_compat import Mapping, Sequence
from ansible.module_utils.six import PY3, string_types
from ansible.module_utils.six.moves import configparser
from ansible.module_utils.parsing.convert_bool import boolean
Expand Down Expand Up @@ -144,6 +144,10 @@ def ensure_type(value, value_type, origin=None):
else:
errmsg = 'pathlist'

elif value_type in ('dict', 'dictionary'):
if not isinstance(value, Mapping):
errmsg = 'dictionary'

elif value_type in ('str', 'string'):
if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode)):
value = unquote(to_text(value, errors='surrogate_or_strict'))
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/doc_fragments/shell_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class ModuleDocFragment(object):
vars:
- name: ansible_async_dir
environment:
type: dict
default: {}
type: list
default: [{}]
description:
- dictionary of environment variables and their values to use when executing commands.
- List of dictionaries of environment variables and their values to use when executing commands.
admin_users:
type: list
default: ['root', 'toor']
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/plugins/doc_fragments/shell_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class ModuleDocFragment(object):
- 'no'
environment:
description:
- Dictionary of environment variables and their values to use when
- List of dictionaries of environment variables and their values to use when
executing commands.
type: dict
default: {}
type: list
default: [{}]
"""

0 comments on commit 8893a24

Please sign in to comment.