From 316d3abb42f8eaf7d272c331fc437dffe76127a6 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Mon, 26 Nov 2018 18:14:46 +0100 Subject: [PATCH] Properly handle FieldAttribute.default if callable (#48992) * Properly handle FieldAttribute.default if callable Fixes #48673 * Add changelog... * Add integration test * Add aliases file (cherry picked from commit 48ffd8789f4f930e2e330ab62781c43fe57ee022) --- .../fragments/48673-fix-omit-on-play-keywords.yaml | 2 ++ lib/ansible/playbook/base.py | 10 ++++++++-- test/integration/targets/omit/48673.yml | 4 ++++ test/integration/targets/omit/aliases | 1 + test/integration/targets/omit/runme.sh | 5 +++++ 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/48673-fix-omit-on-play-keywords.yaml create mode 100644 test/integration/targets/omit/48673.yml create mode 100644 test/integration/targets/omit/aliases create mode 100755 test/integration/targets/omit/runme.sh diff --git a/changelogs/fragments/48673-fix-omit-on-play-keywords.yaml b/changelogs/fragments/48673-fix-omit-on-play-keywords.yaml new file mode 100644 index 0000000000..90d3e27e75 --- /dev/null +++ b/changelogs/fragments/48673-fix-omit-on-play-keywords.yaml @@ -0,0 +1,2 @@ +bugfixes: + - Fix using omit on play keywords (https://github.com/ansible/ansible/issues/48673) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index 48343050dd..945be209fe 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -366,7 +366,10 @@ class FieldAttributeBase(with_metaclass(BaseMeta, object)): # if this evaluated to the omit value, set the value back to # the default specified in the FieldAttribute and move on if omit_value is not None and value == omit_value: - setattr(self, name, attribute.default) + if callable(attribute.default): + setattr(self, name, attribute.default()) + else: + setattr(self, name, attribute.default) continue # and make sure the attribute is of the type it should be @@ -549,7 +552,10 @@ class FieldAttributeBase(with_metaclass(BaseMeta, object)): if name in data: setattr(self, name, data[name]) else: - setattr(self, name, attribute.default) + if callable(attribute.default): + setattr(self, name, attribute.default()) + else: + setattr(self, name, attribute.default) # restore the UUID field setattr(self, '_uuid', data.get('uuid')) diff --git a/test/integration/targets/omit/48673.yml b/test/integration/targets/omit/48673.yml new file mode 100644 index 0000000000..b6d4f31b10 --- /dev/null +++ b/test/integration/targets/omit/48673.yml @@ -0,0 +1,4 @@ +- hosts: localhost + serial: "{{ testing_omitted_variable | default(omit) }}" + tasks: + - debug: diff --git a/test/integration/targets/omit/aliases b/test/integration/targets/omit/aliases new file mode 100644 index 0000000000..b59832142f --- /dev/null +++ b/test/integration/targets/omit/aliases @@ -0,0 +1 @@ +shippable/posix/group3 diff --git a/test/integration/targets/omit/runme.sh b/test/integration/targets/omit/runme.sh new file mode 100755 index 0000000000..962e1f0409 --- /dev/null +++ b/test/integration/targets/omit/runme.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -eux + +ansible-playbook 48673.yml -i ../../inventory -v "$@"