Skip to content

Commit

Permalink
Stop ignoring merge hash behaviour in inventory (ansible#58460)
Browse files Browse the repository at this point in the history
* stop ignoring merge hash behaviour in inventory

fixes ansible#58120

* added porting note

Co-Authored-By: Alicia Cozine <[email protected]>
  • Loading branch information
bcoca and acozine authored Jul 17, 2019
1 parent 0e09800 commit 48d4d6e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/hash_behaviour_inventory.yml
Original file line number Diff line number Diff line change
@@ -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``.
2 changes: 1 addition & 1 deletion docs/docsite/rst/porting_guides/porting_guide_2.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/ansible/inventory/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
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()

Expand Down Expand Up @@ -245,7 +246,10 @@ def set_variable(self, key, value):
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):

Expand Down
6 changes: 5 additions & 1 deletion lib/ansible/inventory/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
__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']
Expand Down Expand Up @@ -137,7 +138,10 @@ def remove_group(self, group):
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
Expand Down

0 comments on commit 48d4d6e

Please sign in to comment.