Skip to content

Commit

Permalink
Config toggle for duplicate dict key warnings (fixes ansible#56799) (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney authored May 23, 2019
1 parent 1da47bf commit 7f0bf0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/56805-duplicate-dict-key-warnings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- added config toggle for YAML duplicate dict key warnings
11 changes: 11 additions & 0 deletions lib/ansible/config/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,17 @@ DOCSITE_ROOT_URL:
ini:
- {key: docsite_root_url, section: defaults}
version_added: "2.8"
DUPLICATE_DICT_KEY_WARNINGS:
name: Toggle warnings for duplicate dict keys in YAML
default: True
description:
- By default Ansible will issue a warning when a duplicate dict key is encountered in YAML.
- These warnings can be silenced by adjusting this setting to False.
env: [{name: ANSIBLE_DUPLICATE_DICT_KEY_WARNINGS}]
ini:
- {key: duplicate_dict_key_warnings, section: defaults}
type: boolean
version_added: "2.9"
ERROR_ON_MISSING_HANDLER:
name: Missing handler error
default: True
Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/parsing/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from yaml.constructor import SafeConstructor, ConstructorError
from yaml.nodes import MappingNode

from ansible import constants as C
from ansible.module_utils._text import to_bytes
from ansible.parsing.yaml.objects import AnsibleMapping, AnsibleSequence, AnsibleUnicode
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode
Expand Down Expand Up @@ -70,8 +71,9 @@ def construct_mapping(self, node, deep=False):
"found unacceptable key (%s)" % exc, key_node.start_mark)

if key in mapping:
display.warning(u'While constructing a mapping from {1}, line {2}, column {3}, found a duplicate dict key ({0}).'
u' Using last defined value only.'.format(key, *mapping.ansible_pos))
if C.DUPLICATE_DICT_KEY_WARNINGS:
display.warning(u'While constructing a mapping from {1}, line {2}, column {3}, found a duplicate dict key ({0}).'
u' Using last defined value only.'.format(key, *mapping.ansible_pos))

value = self.construct_object(value_node, deep=deep)
mapping[key] = value
Expand Down

0 comments on commit 7f0bf0a

Please sign in to comment.