Add option to prepend inherited attributes when extending values

Fixes #18483
This commit is contained in:
James Cammarata 2016-11-14 16:29:13 -06:00
parent 11465134fa
commit 435ca620b2
4 changed files with 16 additions and 11 deletions

View file

@ -482,7 +482,7 @@ class Base(with_metaclass(BaseMeta, object)):
except TypeError as e:
raise AnsibleParserError("Invalid variable name in vars specified for %s: %s" % (self.__class__.__name__, e), obj=ds)
def _extend_value(self, value, new_value):
def _extend_value(self, value, new_value, prepend=False):
'''
Will extend the value given with new_value (and will turn both
into lists if they are not so already). The values are run through
@ -494,7 +494,12 @@ class Base(with_metaclass(BaseMeta, object)):
if not isinstance(new_value, list):
new_value = [ new_value ]
return [i for i,_ in itertools.groupby(value + new_value) if i is not None]
if prepend:
combined = new_value + value
else:
combined = value + new_value
return [i for i,_ in itertools.groupby(combined) if i is not None]
def serialize(self):
'''

View file

@ -273,7 +273,7 @@ class Block(Base, Become, Conditional, Taggable):
def _get_attr_environment(self):
return self._get_parent_attribute('environment', extend=True)
def _get_parent_attribute(self, attr, extend=False):
def _get_parent_attribute(self, attr, extend=False, prepend=False):
'''
Generic logic to get the attribute or parent attribute for a block value.
'''
@ -286,7 +286,7 @@ class Block(Base, Become, Conditional, Taggable):
try:
parent_value = getattr(self._parent, attr, None)
if extend:
value = self._extend_value(value, parent_value)
value = self._extend_value(value, parent_value, prepend)
else:
value = parent_value
except AttributeError:
@ -295,7 +295,7 @@ class Block(Base, Become, Conditional, Taggable):
try:
parent_value = getattr(self._role, attr, None)
if extend:
value = self._extend_value(value, parent_value)
value = self._extend_value(value, parent_value, prepend)
else:
value = parent_value
@ -305,7 +305,7 @@ class Block(Base, Become, Conditional, Taggable):
for dep in dep_chain:
dep_value = getattr(dep, attr, None)
if extend:
value = self._extend_value(value, dep_value)
value = self._extend_value(value, dep_value, prepend)
else:
value = dep_value
@ -317,7 +317,7 @@ class Block(Base, Become, Conditional, Taggable):
try:
parent_value = getattr(self._play, attr, None)
if extend:
value = self._extend_value(value, parent_value)
value = self._extend_value(value, parent_value, prepend)
else:
value = parent_value
except AttributeError:

View file

@ -59,7 +59,7 @@ class Conditional:
if when is None:
when = []
if hasattr(self, '_get_parent_attribute'):
when = self._get_parent_attribute('when', extend=True)
when = self._get_parent_attribute('when', extend=True, prepend=True)
return when
def evaluate_conditional(self, templar, all_vars):
@ -130,5 +130,5 @@ class Conditional:
elif "is defined" in original:
return False
else:
raise AnsibleError("error while evaluating conditional (%s): %s" % (original, e))
raise AnsibleUndefinedVariable("error while evaluating conditional (%s): %s" % (original, e))

View file

@ -388,7 +388,7 @@ class Task(Base, Conditional, Taggable, Become):
if self._parent:
self._parent.set_loader(loader)
def _get_parent_attribute(self, attr, extend=False):
def _get_parent_attribute(self, attr, extend=False, prepend=False):
'''
Generic logic to get the attribute or parent attribute for a task value.
'''
@ -399,7 +399,7 @@ class Task(Base, Conditional, Taggable, Become):
if self._parent and (value is None or extend):
parent_value = getattr(self._parent, attr, None)
if extend:
value = self._extend_value(value, parent_value)
value = self._extend_value(value, parent_value, prepend)
else:
value = parent_value
except KeyError: