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
This commit is contained in:
Brian Coca 2019-01-24 11:51:52 -05:00 committed by GitHub
parent ffac260c66
commit be776daefe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 1 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- add facility for playbook attributes that are not templatable, i.e register

View file

@ -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')

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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" ]

View file

@ -0,0 +1,8 @@
- hosts: testhost
gather_facts: false
vars:
thisshouldwarn: noreally
tasks:
- name: template in register warns
debug: msg=unimportant
register: '{{ thisshouldwarn }}'