From 42cfacf83bc6186fb72d1d724fd97b9aa5a84171 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 28 Jul 2015 16:25:04 -0400 Subject: [PATCH] Switch up the task/host overrides for PlayContext to use the compiled vars dict Fixes #11436 --- lib/ansible/executor/process/worker.py | 2 +- lib/ansible/playbook/play_context.py | 34 ++---------------------- test/units/playbook/test_play_context.py | 5 ++-- 3 files changed, 5 insertions(+), 36 deletions(-) diff --git a/lib/ansible/executor/process/worker.py b/lib/ansible/executor/process/worker.py index 4b2bd13b9c..595cf872e7 100644 --- a/lib/ansible/executor/process/worker.py +++ b/lib/ansible/executor/process/worker.py @@ -111,7 +111,7 @@ class WorkerProcess(multiprocessing.Process): # apply the given task's information to the connection info, # which may override some fields already set by the play or # the options specified on the command line - new_play_context = play_context.set_task_and_host_override(task=task, host=host) + new_play_context = play_context.set_task_and_variable_override(task=task, variables=job_vars) # execute the task and build a TaskResult from the result debug("running TaskExecutor() for %s/%s" % (host, task)) diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py index f6ae473927..fb55c0d6a5 100644 --- a/lib/ansible/playbook/play_context.py +++ b/lib/ansible/playbook/play_context.py @@ -275,24 +275,7 @@ class PlayContext(Base): elif isinstance(options.skip_tags, basestring): self.skip_tags.update(options.skip_tags.split(',')) - #def copy(self, ci): - # ''' - # Copies the connection info from another connection info object, used - # when merging in data from task overrides. - # ''' - # - # for field in self._get_fields(): - # value = getattr(ci, field, None) - # if isinstance(value, dict): - # setattr(self, field, value.copy()) - # elif isinstance(value, set): - # setattr(self, field, value.copy()) - # elif isinstance(value, list): - # setattr(self, field, value[:]) - # else: - # setattr(self, field, value) - - def set_task_and_host_override(self, task, host): + def set_task_and_variable_override(self, task, variables): ''' Sets attributes from the task if they are set, which will override those from the play. @@ -309,8 +292,7 @@ class PlayContext(Base): setattr(new_info, attr, attr_val) # finally, use the MAGIC_VARIABLE_MAPPING dictionary to update this - # connection info object with 'magic' variables from inventory - variables = host.get_vars() + # connection info object with 'magic' variables from the variable list for (attr, variable_names) in MAGIC_VARIABLE_MAPPING.iteritems(): for variable_name in variable_names: if variable_name in variables: @@ -388,18 +370,6 @@ class PlayContext(Base): return cmd - #def _get_fields(self): - # return [i for i in self.__dict__.keys() if i[:1] != '_'] - - #def post_validate(self, templar): - # ''' - # Finalizes templated values which may be set on this objects fields. - # ''' - # - # for field in self._get_fields(): - # value = templar.template(getattr(self, field)) - # setattr(self, field, value) - def update_vars(self, variables): ''' Adds 'magic' variables relating to connections to the variable dictionary provided. diff --git a/test/units/playbook/test_play_context.py b/test/units/playbook/test_play_context.py index 1eade6f540..fe475833ca 100644 --- a/test/units/playbook/test_play_context.py +++ b/test/units/playbook/test_play_context.py @@ -93,14 +93,13 @@ class TestPlayContext(unittest.TestCase): mock_task.become_pass = 'mocktaskpass' mock_task.no_log = False - mock_host = MagicMock() - mock_host.get_vars.return_value = dict( + all_vars = dict( ansible_connection = 'mock_inventory', ansible_ssh_port = 4321, ) play_context = PlayContext(play=mock_play, options=options) - play_context = play_context.set_task_and_host_override(task=mock_task, host=mock_host) + play_context = play_context.set_task_and_variable_override(task=mock_task, variables=all_vars) self.assertEqual(play_context.connection, 'mock_inventory') self.assertEqual(play_context.remote_user, 'mocktask') self.assertEqual(play_context.port, 4321)