Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tags: fix unexpected exception for tags as dict #53529

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/53529-fail_for_unexpected_tags.yml
@@ -0,0 +1,2 @@
bugfixes:
- "Fix an 'Unexpected Exception' if tags are not as expected"
5 changes: 3 additions & 2 deletions lib/ansible/playbook/taggable.py
Expand Up @@ -51,10 +51,11 @@ def evaluate_tags(self, only_tags, skip_tags, all_vars):

_temp_tags = set()
for tag in tags:
if isinstance(tag, list):
_temp_tags.update(tag)
if not isinstance(tag, (string_types, int)):
raise AnsibleError('tags must be specified as int or string', obj=tag)
else:
_temp_tags.add(tag)

tags = _temp_tags
self.tags = list(tags)
else:
Expand Down
21 changes: 21 additions & 0 deletions test/integration/targets/tags/runme.sh
Expand Up @@ -47,3 +47,24 @@ export LC_ALL=en_US.UTF-8
# Run templated tags
[ "$("${COMMAND[@]}" --tags tag3 | grep -F Task_with | xargs)" = \
"Task_with_always_tag TAGS: [always] Task_with_templated_tags TAGS: [tag3]" ]

resmo marked this conversation as resolved.
Show resolved Hide resolved
## Run tags as int
#[ "$("${COMMAND[@]}" --tags 1 | grep -F Task_with | xargs)" = \
#"Task_with_always_tag TAGS: [always] Task_as_int TAGS: [1]" ]

## Run tags as list of int
#[ "$("${COMMAND[@]}" --tags 2 | grep -F Task_with | xargs)" = \
#"Task_with_always_tag TAGS: [always] Task_as_int_list TAGS: [2]" ]

# Tests expected to fail
OUT=$(ansible-playbook -i ../../inventory test_unexpected_dict_tags.yml -v --list-tasks 2>&1 | grep 'ERROR! tags must be specified as a int or string')
if [[ -z "$OUT" ]]; then
echo "Failed unexpected for tags as dict"
exit 1
fi

OUT=$(ansible-playbook -i ../../inventory test_unexpected_list_tags.yml -v --list-tasks 2>&1 | grep 'ERROR! tags must be specified as a int or string')
if [[ -z "$OUT" ]]; then
echo "Failed unexpected for tags as list"
exit 1
fi
7 changes: 7 additions & 0 deletions test/integration/targets/tags/test_tags.yml
Expand Up @@ -32,3 +32,10 @@
- name: Task_with_templated_tags
debug: msg=templated
tags: "{{ the_tags }}"
- name: Task_as_int
debug: msg=int_tag
tags: 1
- name: Task_as_int_list
debug: msg=tags_int_list
tags:
- 2
10 changes: 10 additions & 0 deletions test/integration/targets/tags/test_unexpected_dict_tags.yml
@@ -0,0 +1,10 @@
---
- name: verify unexpected tags fail as expected
hosts: localhost
gather_facts: False
connection: local
tasks:
- name: Task_with_unexpected_dict_tags
debug: msg=unexpected_dict_tags
tags:
- { key: one, value: two }
10 changes: 10 additions & 0 deletions test/integration/targets/tags/test_unexpected_list_tags.yml
@@ -0,0 +1,10 @@
---
- name: verify unexpected tags fail as expected
hosts: localhost
gather_facts: False
connection: local
tasks:
- name: Task_with_unexpected_list_tags
debug: msg=unexpected_list_tags
tags:
- [ one, two, three ]