1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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,
# 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))

View file

@ -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.

View file

@ -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)