vars: handle exception in combine_vars (#81700)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/81729/head
Abhijeet Kasurde 1 year ago committed by GitHub
parent f7234968d2
commit 0ea40e09d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
---
bugfixes:
- vars - handle exception while combining VarsWithSources and dict (https://github.com/ansible/ansible/issues/81659).

@ -85,11 +85,11 @@ def combine_vars(a, b, merge=None):
if merge or merge is None and C.DEFAULT_HASH_BEHAVIOUR == "merge": if merge or merge is None and C.DEFAULT_HASH_BEHAVIOUR == "merge":
return merge_hash(a, b) return merge_hash(a, b)
else:
# HASH_BEHAVIOUR == 'replace' # HASH_BEHAVIOUR == 'replace'
_validate_mutable_mappings(a, b) _validate_mutable_mappings(a, b)
result = a | b result = a | b
return result return result
def merge_hash(x, y, recursive=True, list_merge='replace'): def merge_hash(x, y, recursive=True, list_merge='replace'):

@ -786,3 +786,22 @@ class VarsWithSources(MutableMapping):
def copy(self): def copy(self):
return VarsWithSources.new_vars_with_sources(self.data.copy(), self.sources.copy()) return VarsWithSources.new_vars_with_sources(self.data.copy(), self.sources.copy())
def __or__(self, other):
if isinstance(other, MutableMapping):
c = self.data.copy()
c.update(other)
return c
return NotImplemented
def __ror__(self, other):
if isinstance(other, MutableMapping):
c = self.__class__()
c.update(other)
c.update(self.data)
return c
return NotImplemented
def __ior__(self, other):
self.data.update(other)
return self.data

@ -27,6 +27,7 @@ from unittest import mock
from units.compat import unittest from units.compat import unittest
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.utils.vars import combine_vars, merge_hash from ansible.utils.vars import combine_vars, merge_hash
from ansible.vars.manager import VarsWithSources
class TestVariableUtils(unittest.TestCase): class TestVariableUtils(unittest.TestCase):
@ -42,6 +43,11 @@ class TestVariableUtils(unittest.TestCase):
b=dict(b=2), b=dict(b=2),
result=dict(a=1, b=2), result=dict(a=1, b=2),
), ),
dict(
a=dict(a=1),
b=VarsWithSources().new_vars_with_sources(dict(b=2), dict(b='task vars')),
result=dict(a=1, b=2),
),
dict( dict(
a=dict(a=1, c=dict(foo='bar')), a=dict(a=1, c=dict(foo='bar')),
b=dict(b=2, c=dict(baz='bam')), b=dict(b=2, c=dict(baz='bam')),
@ -59,6 +65,11 @@ class TestVariableUtils(unittest.TestCase):
b=dict(b=2), b=dict(b=2),
result=dict(a=1, b=2) result=dict(a=1, b=2)
), ),
dict(
a=dict(a=1),
b=VarsWithSources().new_vars_with_sources(dict(b=2), dict(b='task vars')),
result=dict(a=1, b=2),
),
dict( dict(
a=dict(a=1, c=dict(foo='bar')), a=dict(a=1, c=dict(foo='bar')),
b=dict(b=2, c=dict(baz='bam')), b=dict(b=2, c=dict(baz='bam')),

Loading…
Cancel
Save