From c4c3cc315d1c8f34f2b52b0a3d1af7bdbe5c9ae4 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 19 Nov 2014 11:50:02 -0800 Subject: [PATCH] Transform both values of a task name into a byte str prior to comparing Fixes #9571 --- lib/ansible/callbacks.py | 2 ++ lib/ansible/utils/__init__.py | 11 +++++++++++ test/integration/Makefile | 5 ++--- test/integration/unicode.yml | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index d6dfb3c11c9..a4b62fb0054 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -603,11 +603,13 @@ class PlaybookCallbacks(object): call_callback_module('playbook_on_no_hosts_remaining') def on_task_start(self, name, is_conditional): + name = utils.to_bytes(name) msg = "TASK: [%s]" % name if is_conditional: msg = "NOTIFIED: [%s]" % name if hasattr(self, 'start_at'): + self.start_at = utils.to_bytes(self.start_at) if name == self.start_at or fnmatch.fnmatch(name, self.start_at): # we found out match, we can get rid of this now del self.start_at diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 06ca8144cc8..674ca1cb112 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -1265,13 +1265,24 @@ def make_su_cmd(su_user, executable, cmd): ) return ('/bin/sh -c ' + pipes.quote(sudocmd), None, success_key) +# For v2, consider either using kitchen or copying my code from there for +# to_unicode and to_bytes handling (TEK) _TO_UNICODE_TYPES = (unicode, type(None)) def to_unicode(value): + # Use with caution -- this function is not encoding safe (non-utf-8 values + # will cause tracebacks if they contain bytes from 0x80-0xff inclusive) if isinstance(value, _TO_UNICODE_TYPES): return value return value.decode("utf-8") +def to_bytes(value): + # Note: value is assumed to be a basestring to mirror to_unicode. Better + # implementations (like kitchen.text.converters.to_bytes) bring that check + # into the function + if isinstance(value, str): + return value + return value.encode('utf-8') def get_diff(diff): # called by --diff usage in playbook and runner via callbacks diff --git a/test/integration/Makefile b/test/integration/Makefile index ac4aafe3f07..b732eb02f8b 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -34,13 +34,12 @@ includes: unicode: ansible-playbook unicode.yml -i $(INVENTORY) -e @$(VARS_FILE) -v $(TEST_FLAGS) + # Test the start-at-task flag #9571 + ansible-playbook unicode.yml -i $(INVENTORY) -e @$(VARS_FILE) -v --start-at-task '*¶' -e 'start_at_task=True' $(TEST_FLAGS) non_destructive: ansible-playbook non_destructive.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) -mine: - ansible-playbook mine.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) - destructive: ansible-playbook destructive.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) diff --git a/test/integration/unicode.yml b/test/integration/unicode.yml index 69c737a8a33..60fb14214bc 100644 --- a/test/integration/unicode.yml +++ b/test/integration/unicode.yml @@ -41,3 +41,20 @@ tasks: - debug: msg='Unicode is a good thing ™' - debug: msg=АБВГД + +# Run this test by adding to the CLI: -e start_at_task=True --start-at-task '*¶' +- name: 'Show that we can skip to unicode named tasks' + hosts: localhost + gather_facts: false + vars: + flag: 'original' + start_at_task: False + tasks: + - name: 'Override flag var' + set_fact: flag='new' + + - name: 'A unicode task at the end of the playbook: ¶' + assert: + that: + - 'flag == "original"' + when: start_at_task|bool