From 1425e3597be3c186ae50a7e488bc1f61c85a274f Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Fri, 21 Aug 2020 11:08:49 -0400 Subject: [PATCH] Allow meta tasks to use tags (#67508) * Support tags for explicit meta tasks Add --list-tasks tests for meta tags Add breaking_changes changelog fragment and porting guide --- changelogs/fragments/67508-meta-task-tags.yaml | 2 ++ .../porting_guides/porting_guide_base_2.11.rst | 1 + lib/ansible/cli/playbook.py | 2 +- lib/ansible/modules/meta.py | 1 - lib/ansible/playbook/block.py | 2 +- test/integration/targets/tags/runme.sh | 16 ++++++++++++---- test/integration/targets/tags/test_tags.yml | 3 +++ 7 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/67508-meta-task-tags.yaml diff --git a/changelogs/fragments/67508-meta-task-tags.yaml b/changelogs/fragments/67508-meta-task-tags.yaml new file mode 100644 index 00000000000..df56076dbb3 --- /dev/null +++ b/changelogs/fragments/67508-meta-task-tags.yaml @@ -0,0 +1,2 @@ +breaking_changes: + - The ``meta`` module now supports tags for user-defined tasks. Internal ``meta`` tasks continue to always run. (https://github.com/ansible/ansible/issues/64558) diff --git a/docs/docsite/rst/porting_guides/porting_guide_base_2.11.rst b/docs/docsite/rst/porting_guides/porting_guide_base_2.11.rst index 155f2069d0b..912e9f6ff0c 100644 --- a/docs/docsite/rst/porting_guides/porting_guide_base_2.11.rst +++ b/docs/docsite/rst/porting_guides/porting_guide_base_2.11.rst @@ -39,6 +39,7 @@ Modules ======= * The ``apt_key`` module has explicitly defined ``file`` as mutually exclusive with ``data``, ``keyserver`` and ``url``. They cannot be used together anymore. +* The ``meta`` module now supports tags for user-defined tasks. Set the task's tags to 'always' to maintain the previous behavior. Internal ``meta`` tasks continue to always run. Modules removed diff --git a/lib/ansible/cli/playbook.py b/lib/ansible/cli/playbook.py index 3429a283964..49c24f7353e 100644 --- a/lib/ansible/cli/playbook.py +++ b/lib/ansible/cli/playbook.py @@ -162,7 +162,7 @@ class PlaybookCLI(CLI): if isinstance(task, Block): taskmsg += _process_block(task) else: - if task.action == 'meta': + if task.action == 'meta' and task.implicit: continue all_tags.update(task.tags) diff --git a/lib/ansible/modules/meta.py b/lib/ansible/modules/meta.py index 650fdd581db..8da4fe8d38f 100644 --- a/lib/ansible/modules/meta.py +++ b/lib/ansible/modules/meta.py @@ -40,7 +40,6 @@ notes: - C(clear_facts) will remove the persistent facts from M(ansible.builtin.set_fact) using C(cacheable=True), but not the current host variable it creates for the current run. - Looping on meta tasks is not supported. - - Skipping C(meta) tasks using tags is not supported. - This module is also supported for Windows targets. seealso: - module: ansible.builtin.assert diff --git a/lib/ansible/playbook/block.py b/lib/ansible/playbook/block.py index 5f766e00836..cab89dd9925 100644 --- a/lib/ansible/playbook/block.py +++ b/lib/ansible/playbook/block.py @@ -374,7 +374,7 @@ class Block(Base, Conditional, CollectionSearch, Taggable): filtered_block = evaluate_block(task) if filtered_block.has_tasks(): tmp_list.append(filtered_block) - elif (task.action == 'meta' or + elif ((task.action == 'meta' and task.implicit) or (task.action == 'include' and task.evaluate_tags([], self._play.skip_tags, all_vars=all_vars)) or task.evaluate_tags(self._play.only_tags, self._play.skip_tags, all_vars=all_vars)): tmp_list.append(task) diff --git a/test/integration/targets/tags/runme.sh b/test/integration/targets/tags/runme.sh index 79c51e27efa..2984995209e 100755 --- a/test/integration/targets/tags/runme.sh +++ b/test/integration/targets/tags/runme.sh @@ -14,7 +14,7 @@ export LC_ALL=en_US.UTF-8 # Run everything by default [ "$("${COMMAND[@]}" | grep -F Task_with | xargs)" = \ -"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3]" ] +"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3] Task_with_meta_tags TAGS: [meta_tag]" ] # Run the exact tags, and always [ "$("${COMMAND[@]}" --tags tag | grep -F Task_with | xargs)" = \ @@ -22,11 +22,15 @@ export LC_ALL=en_US.UTF-8 # Skip one tag [ "$("${COMMAND[@]}" --skip-tags tag | grep -F Task_with | xargs)" = \ -"Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3]" ] +"Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3] Task_with_meta_tags TAGS: [meta_tag]" ] # Skip a unicode tag [ "$("${COMMAND[@]}" --skip-tags 'くらとみ' | grep -F Task_with | xargs)" = \ -"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3]" ] +"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3] Task_with_meta_tags TAGS: [meta_tag]" ] + +# Skip a meta task tag +[ "$("${COMMAND[@]}" --skip-tags meta_tag | grep -F Task_with | xargs)" = \ +"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_without_tag TAGS: [] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3]" ] # Run just a unicode tag and always [ "$("${COMMAND[@]}" --tags 'くらとみ' | grep -F Task_with | xargs)" = \ @@ -48,9 +52,13 @@ export LC_ALL=en_US.UTF-8 [ "$("${COMMAND[@]}" --tags tag3 | grep -F Task_with | xargs)" = \ "Task_with_always_tag TAGS: [always] Task_with_templated_tags TAGS: [tag3]" ] +# Run meta tags +[ "$("${COMMAND[@]}" --tags meta_tag | grep -F Task_with | xargs)" = \ +"Task_with_always_tag TAGS: [always] Task_with_meta_tags TAGS: [meta_tag]" ] + # Run tagged [ "$("${COMMAND[@]}" --tags tagged | grep -F Task_with | xargs)" = \ -"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3]" ] +"Task_with_tag TAGS: [tag] Task_with_always_tag TAGS: [always] Task_with_unicode_tag TAGS: [くらとみ] Task_with_list_of_tags TAGS: [café, press] Task_with_csv_tags TAGS: [tag1, tag2] Task_with_templated_tags TAGS: [tag3] Task_with_meta_tags TAGS: [meta_tag]" ] # Run untagged [ "$("${COMMAND[@]}" --tags untagged | grep -F Task_with | xargs)" = \ diff --git a/test/integration/targets/tags/test_tags.yml b/test/integration/targets/tags/test_tags.yml index 76ac5ba4bd4..f0f9a72d73d 100644 --- a/test/integration/targets/tags/test_tags.yml +++ b/test/integration/targets/tags/test_tags.yml @@ -31,3 +31,6 @@ - name: Task_with_templated_tags debug: msg=templated tags: "{{ the_tags }}" + - name: Task_with_meta_tags + meta: reset_connection + tags: meta_tag