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>
This commit is contained in:
Brian Coca 2019-07-17 11:16:56 -04:00 committed by GitHub
parent 0e09800a9a
commit 48d4d6ec69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 4 deletions

View file

@ -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``.

View file

@ -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

View file

@ -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):

View file

@ -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