Switch up the task/host overrides for PlayContext to use the compiled vars dict

Fixes #11436
This commit is contained in:
James Cammarata 2015-07-28 16:25:04 -04:00
parent 2d2ec058c8
commit 42cfacf83b
3 changed files with 5 additions and 36 deletions

View file

@ -111,7 +111,7 @@ class WorkerProcess(multiprocessing.Process):
# apply the given task's information to the connection info, # apply the given task's information to the connection info,
# which may override some fields already set by the play or # which may override some fields already set by the play or
# the options specified on the command line # 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 # execute the task and build a TaskResult from the result
debug("running TaskExecutor() for %s/%s" % (host, task)) debug("running TaskExecutor() for %s/%s" % (host, task))

View file

@ -275,24 +275,7 @@ class PlayContext(Base):
elif isinstance(options.skip_tags, basestring): elif isinstance(options.skip_tags, basestring):
self.skip_tags.update(options.skip_tags.split(',')) self.skip_tags.update(options.skip_tags.split(','))
#def copy(self, ci): def set_task_and_variable_override(self, task, variables):
# '''
# 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):
''' '''
Sets attributes from the task if they are set, which will override Sets attributes from the task if they are set, which will override
those from the play. those from the play.
@ -309,8 +292,7 @@ class PlayContext(Base):
setattr(new_info, attr, attr_val) setattr(new_info, attr, attr_val)
# finally, use the MAGIC_VARIABLE_MAPPING dictionary to update this # finally, use the MAGIC_VARIABLE_MAPPING dictionary to update this
# connection info object with 'magic' variables from inventory # connection info object with 'magic' variables from the variable list
variables = host.get_vars()
for (attr, variable_names) in MAGIC_VARIABLE_MAPPING.iteritems(): for (attr, variable_names) in MAGIC_VARIABLE_MAPPING.iteritems():
for variable_name in variable_names: for variable_name in variable_names:
if variable_name in variables: if variable_name in variables:
@ -388,18 +370,6 @@ class PlayContext(Base):
return cmd 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): def update_vars(self, variables):
''' '''
Adds 'magic' variables relating to connections to the variable dictionary provided. Adds 'magic' variables relating to connections to the variable dictionary provided.

View file

@ -93,14 +93,13 @@ class TestPlayContext(unittest.TestCase):
mock_task.become_pass = 'mocktaskpass' mock_task.become_pass = 'mocktaskpass'
mock_task.no_log = False mock_task.no_log = False
mock_host = MagicMock() all_vars = dict(
mock_host.get_vars.return_value = dict(
ansible_connection = 'mock_inventory', ansible_connection = 'mock_inventory',
ansible_ssh_port = 4321, ansible_ssh_port = 4321,
) )
play_context = PlayContext(play=mock_play, options=options) 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.connection, 'mock_inventory')
self.assertEqual(play_context.remote_user, 'mocktask') self.assertEqual(play_context.remote_user, 'mocktask')
self.assertEqual(play_context.port, 4321) self.assertEqual(play_context.port, 4321)