From 48d4d6ec691b2c749054aa5117761f956d762f1c Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 17 Jul 2019 11:16:56 -0400 Subject: [PATCH] Stop ignoring merge hash behaviour in inventory (#58460) * stop ignoring merge hash behaviour in inventory fixes #58120 * added porting note Co-Authored-By: Alicia Cozine <879121+acozine@users.noreply.github.com> --- changelogs/fragments/hash_behaviour_inventory.yml | 2 ++ docs/docsite/rst/porting_guides/porting_guide_2.9.rst | 2 +- lib/ansible/inventory/group.py | 8 ++++++-- lib/ansible/inventory/host.py | 6 +++++- 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/hash_behaviour_inventory.yml 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