diff --git a/changelogs/fragments/hash_behaviour_inventory.yml b/changelogs/fragments/hash_behaviour_inventory.yml new file mode 100644 index 00000000000..d289bba9520 --- /dev/null +++ b/changelogs/fragments/hash_behaviour_inventory.yml @@ -0,0 +1,2 @@ +bugfixes: + - Inventory sources now respect setting ``hash_behaviour``. Previously each new inventory source would overwrite existing vars, even when ``hash_behavior`` was set to ``merge``. diff --git a/docs/docsite/rst/porting_guides/porting_guide_2.9.rst b/docs/docsite/rst/porting_guides/porting_guide_2.9.rst index 9c25696b57f..d63a38b59c5 100644 --- a/docs/docsite/rst/porting_guides/porting_guide_2.9.rst +++ b/docs/docsite/rst/porting_guides/porting_guide_2.9.rst @@ -19,7 +19,7 @@ This document is part of a collection on porting. The complete list of porting g Playbook ======== -No notable changes + * ``hash_behaviour`` now affects inventory sources. If you have it set to ``merge``, the data you get from inventory might change and you will have to update playbooks accordingly. If you're using the default setting (``overwrite``), you will see no changes. Inventory was ignoring this setting. Command Line diff --git a/lib/ansible/inventory/group.py b/lib/ansible/inventory/group.py index 6d5e19ef592..4d1d6ab0d6c 100644 --- a/lib/ansible/inventory/group.py +++ b/lib/ansible/inventory/group.py @@ -22,8 +22,9 @@ from itertools import chain from ansible import constants as C from ansible.errors import AnsibleError from ansible.module_utils._text import to_native, to_text - +from ansible.module_utils.common._collections_compat import Mapping, MutableMapping from ansible.utils.display import Display +from ansible.utils.vars import combine_vars display = Display() @@ -245,7 +246,10 @@ class Group: if key == 'ansible_group_priority': self.set_priority(int(value)) else: - self.vars[key] = value + if key in self.vars and isinstance(self.vars[key], MutableMapping) and isinstance(value, Mapping): + self.vars[key] = combine_vars(self.vars[key], value) + else: + self.vars[key] = value def clear_hosts_cache(self): diff --git a/lib/ansible/inventory/host.py b/lib/ansible/inventory/host.py index 43272739454..91bb5570b0a 100644 --- a/lib/ansible/inventory/host.py +++ b/lib/ansible/inventory/host.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible.inventory.group import Group +from ansible.module_utils.common._collections_compat import Mapping, MutableMapping from ansible.utils.vars import combine_vars, get_unique_id __all__ = ['Host'] @@ -137,7 +138,10 @@ class Host: self.remove_group(oldg) def set_variable(self, key, value): - self.vars[key] = value + if key in self.vars and isinstance(self.vars[key], MutableMapping) and isinstance(value, Mapping): + self.vars[key] = combine_vars(self.vars[key], value) + else: + self.vars[key] = value def get_groups(self): return self.groups