omit keyword should reset to context (#78917)

* omit keyword should reset to context

  ensure we use context/inheritance when calculating value,
  using default only when context is unavailable.

  fixes #75692
pull/78950/head
Brian Coca 2 years ago committed by GitHub
parent 20c1252212
commit 9650ddb11c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- omit on keywords was resetting to default value, ignoring inheritance.

@ -362,10 +362,7 @@ class FieldAttributeBase:
# Resolve extended groups last, after caching the group in case they recursively refer to each other # Resolve extended groups last, after caching the group in case they recursively refer to each other
for include_group in include_groups: for include_group in include_groups:
if not AnsibleCollectionRef.is_valid_fqcr(include_group): if not AnsibleCollectionRef.is_valid_fqcr(include_group):
include_group_collection = collection_name
include_group = collection_name + '.' + include_group include_group = collection_name + '.' + include_group
else:
include_group_collection = '.'.join(include_group.split('.')[0:2])
dummy, group_actions = self._resolve_group(include_group, mandatory=False) dummy, group_actions = self._resolve_group(include_group, mandatory=False)
@ -481,6 +478,26 @@ class FieldAttributeBase:
value.post_validate(templar=templar) value.post_validate(templar=templar)
return value return value
def set_to_context(self, name):
attribute = self.fattributes[name]
if isinstance(attribute, NonInheritableFieldAttribute):
self.set_to_default(name)
else:
try:
setattr(self, name, self._get_parent_attribute(name))
except AttributeError:
# mostly playcontext as only tasks/handlers really resolve to parent
self.set_to_default(name)
def set_to_default(self, name):
attribute = self.fattributes[name]
if callable(attribute.default):
setattr(self, name, attribute.default())
else:
setattr(self, name, attribute.default)
def post_validate(self, templar): def post_validate(self, templar):
''' '''
we can't tell that everything is of the right type until we have we can't tell that everything is of the right type until we have
@ -524,13 +541,11 @@ class FieldAttributeBase:
# if the attribute contains a variable, template it now # if the attribute contains a variable, template it now
value = templar.template(getattr(self, name)) value = templar.template(getattr(self, name))
# if this evaluated to the omit value, set the value back to # If this evaluated to the omit value, set the value back to inherited by context
# the default specified in the FieldAttribute and move on # or default specified in the FieldAttribute and move on
if omit_value is not None and value == omit_value: if omit_value is not None and value == omit_value:
if callable(attribute.default): self.set_to_context(name)
setattr(self, name, attribute.default()) self._finalized = True
else:
setattr(self, name, attribute.default)
continue continue
# and make sure the attribute is of the type it should be # and make sure the attribute is of the type it should be
@ -686,7 +701,7 @@ class FieldAttributeBase:
if name in data: if name in data:
setattr(self, name, data[name]) setattr(self, name, data[name])
else: else:
setattr(self, name, attribute.default) self.set_to_context(name)
# restore the UUID field # restore the UUID field
setattr(self, '_uuid', data.get('uuid')) setattr(self, '_uuid', data.get('uuid'))

@ -0,0 +1,29 @@
- name: omit should reset to 'absent' or same context, not just 'default' value
hosts: testhost
gather_facts: false
become: yes
become_user: nobody
roles:
- name: setup_test_user
tasks:
- shell: whoami
register: inherited
- shell: whoami
register: explicit_no
become: false
- shell: whoami
register: omited_inheritance
become: '{{ omit }}'
- shell: whoami
register: explicit_yes
become: yes
- name: ensure omit works with inheritance
assert:
that:
- inherited.stdout == omited_inheritance.stdout
- inherited.stdout == explicit_yes.stdout
- inherited.stdout != explicit_no.stdout

@ -1,2 +1,3 @@
shippable/posix/group5 shippable/posix/group5
needs/target/setup_test_user
context/controller context/controller

@ -2,4 +2,4 @@
set -eux set -eux
ansible-playbook 48673.yml -i ../../inventory -v "$@" ANSIBLE_ROLES_PATH=../ ansible-playbook 48673.yml 75692.yml -i ../../inventory -v "$@"

Loading…
Cancel
Save