tags warn wwhen using reserved tags (#85631)

pull/84914/merge
Brian Coca 3 months ago committed by GitHub
parent 8b6446f2d5
commit 75ad1f8d6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
minor_changes:
- tags now warn when using reserved keywords.

@ -0,0 +1,2 @@
minor_changes:
- ansible now warns if you use reserved tags that were only meant for selection and not for use in play.

@ -19,11 +19,14 @@ from __future__ import annotations
import typing as t
from ansible._internal._templating._engine import TemplateEngine
from ansible.errors import AnsibleError
from ansible.module_utils.common.sentinel import Sentinel
from ansible.module_utils._internal._datatag import AnsibleTagHelper
from ansible.playbook.attribute import FieldAttribute
from ansible._internal._templating._engine import TemplateEngine
from ansible.utils.display import Display
_display = Display()
def _flatten_tags(tags: list[str | int]) -> list[str | int]:
@ -38,17 +41,25 @@ def _flatten_tags(tags: list[str | int]) -> list[str | int]:
class Taggable:
_RESERVED = frozenset(['tagged', 'all', 'untagged'])
untagged = frozenset(['untagged'])
tags = FieldAttribute(isa='list', default=list, listof=(str, int), extend=True)
def _load_tags(self, attr, ds):
tags = None
if isinstance(ds, list):
return ds
tags = ds
elif isinstance(ds, str):
tags = [AnsibleTagHelper.tag_copy(ds, item.strip()) for item in ds.split(',')]
if tags is None:
raise AnsibleError('tags must be specified as a list', obj=ds)
if isinstance(ds, str):
return [AnsibleTagHelper.tag_copy(ds, item.strip()) for item in ds.split(',')]
if found := self._RESERVED.intersection(tags):
_display.warning(f"Found reserved tagnames in tags: {list(found)!r}, we do not recommend doing this as it might give unexpected results", obj=ds)
raise AnsibleError('tags must be specified as a list', obj=ds)
return tags
def _get_all_taggable_objects(self) -> t.Iterable[Taggable]:
obj = self

@ -99,4 +99,8 @@ ansible-playbook test_template_role_tags.yml --tags tag1 "$@" 2>&1 | tee out.txt
[ "$(grep out.txt -ce 'Tagged_task')" = "1" ]; rm out.txt
ansible-playbook test_template_role_tags.yml --skip-tags tag1 "$@" 2>&1 | tee out.txt
[ "$(grep out.txt -ce 'Tagged_task')" = "0" ]; rm out.txt
[ "$(grep out.txt -ce 'Tagged_task')" = "0" ];
[ "$(grep out.txt -ce 'Found reserved tagsnames')" = "0" ]; rm out.txt
ansible-playbook warn_reserved.yml "$@" 2>&1 | tee out.txt
[ "$(grep out.txt -ce 'Found reserved tagnames')" = "1" ]; rm out.txt

@ -0,0 +1,6 @@
- hosts: all
gather_facts: false
tasks:
- debug: msg=not tagged
- debug: msg=taggged
tags: tagged
Loading…
Cancel
Save