From be776daefed047f0bd45d7441efde046677fd417 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 24 Jan 2019 11:51:52 -0500 Subject: [PATCH] add static facility and apply to register (#49737) * add static facility and apply to register * added warning * added test for templated register * test register 'static' status * rely on subshell to deal with quote context * use corrects pb for test * bring constants back cause new code in devel --- changelogs/fragments/static_attributes.yml | 2 ++ lib/ansible/playbook/attribute.py | 2 ++ lib/ansible/playbook/base.py | 8 ++++++++ lib/ansible/playbook/task.py | 2 +- .../targets/templating_settings/dont_warn_register.yml | 6 ++++++ test/integration/targets/templating_settings/runme.sh | 2 ++ .../targets/templating_settings/warn_on_register.yml | 8 ++++++++ 7 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/static_attributes.yml create mode 100644 test/integration/targets/templating_settings/dont_warn_register.yml create mode 100644 test/integration/targets/templating_settings/warn_on_register.yml diff --git a/changelogs/fragments/static_attributes.yml b/changelogs/fragments/static_attributes.yml new file mode 100644 index 00000000000..6aa56c62bfd --- /dev/null +++ b/changelogs/fragments/static_attributes.yml @@ -0,0 +1,2 @@ +minor_changes: + - add facility for playbook attributes that are not templatable, i.e register diff --git a/lib/ansible/playbook/attribute.py b/lib/ansible/playbook/attribute.py index b7dcce3e184..20370b9826e 100644 --- a/lib/ansible/playbook/attribute.py +++ b/lib/ansible/playbook/attribute.py @@ -41,6 +41,7 @@ class Attribute: alias=None, extend=False, prepend=False, + static=False, ): """ @@ -86,6 +87,7 @@ class Attribute: self.alias = alias self.extend = extend self.prepend = prepend + self.static = static if default is not None and self.isa in _CONTAINERS and not callable(default): raise TypeError('defaults for FieldAttribute may not be mutable, please provide a callable instead') diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index f1b7ad6ac83..c8eae37b941 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -14,6 +14,7 @@ from functools import partial from jinja2.exceptions import UndefinedError from ansible import constants as C + from ansible.module_utils.six import iteritems, string_types, with_metaclass from ansible.module_utils.parsing.convert_bool import boolean from ansible.errors import AnsibleParserError, AnsibleUndefinedVariable, AnsibleAssertionError @@ -350,6 +351,13 @@ class FieldAttributeBase(with_metaclass(BaseMeta, object)): for (name, attribute) in iteritems(self._valid_attrs): + if attribute.static: + value = getattr(self, name) + if templar.is_template(value): + display.warning('"%s" is not templatable, but we found: %s, ' + 'it will not be templated and will be used "as is".' % (name, value)) + continue + if getattr(self, name) is None: if not attribute.required: continue diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index a0b775dd997..80854f3fb92 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -80,7 +80,7 @@ class Task(Base, Conditional, Taggable, Become): _loop_control = FieldAttribute(isa='class', class_type=LoopControl, inherit=False) _notify = FieldAttribute(isa='list') _poll = FieldAttribute(isa='int', default=10) - _register = FieldAttribute(isa='string') + _register = FieldAttribute(isa='string', static=True) _retries = FieldAttribute(isa='int', default=3) _until = FieldAttribute(isa='list', default=list) diff --git a/test/integration/targets/templating_settings/dont_warn_register.yml b/test/integration/targets/templating_settings/dont_warn_register.yml new file mode 100644 index 00000000000..277ce788e8c --- /dev/null +++ b/test/integration/targets/templating_settings/dont_warn_register.yml @@ -0,0 +1,6 @@ +- hosts: testhost + gather_facts: false + tasks: + - name: template in register warns, but no template should not + debug: msg=unimportant + register: thisshouldnotwarn diff --git a/test/integration/targets/templating_settings/runme.sh b/test/integration/targets/templating_settings/runme.sh index 4af73bfd94f..027619d5718 100755 --- a/test/integration/targets/templating_settings/runme.sh +++ b/test/integration/targets/templating_settings/runme.sh @@ -3,3 +3,5 @@ set -eux ansible-playbook test_templating_settings.yml -i ../../inventory -v "$@" +ansible-playbook warn_on_register.yml -i ../../inventory -v "$@" 2>&1| grep 'is not templatable, but we found' +[ "$(ansible-playbook dont_warn_register.yml -i ../../inventory -v "$@" 2>&1| grep -c 'is not templatable, but we found')" == "0" ] diff --git a/test/integration/targets/templating_settings/warn_on_register.yml b/test/integration/targets/templating_settings/warn_on_register.yml new file mode 100644 index 00000000000..1790451f920 --- /dev/null +++ b/test/integration/targets/templating_settings/warn_on_register.yml @@ -0,0 +1,8 @@ +- hosts: testhost + gather_facts: false + vars: + thisshouldwarn: noreally + tasks: + - name: template in register warns + debug: msg=unimportant + register: '{{ thisshouldwarn }}'