From eae9a406ad304c383321846e712aaa6328014176 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 20 Jan 2015 01:16:19 -0600 Subject: [PATCH] Fixing v2 code for test_yum and added capability to squash items --- v2/ansible/executor/process/result.py | 3 +-- v2/ansible/executor/task_executor.py | 26 ++++++++++++++++++++------ v2/ansible/executor/task_result.py | 3 ++- v2/bin/ansible-playbook | 3 ++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/v2/ansible/executor/process/result.py b/v2/ansible/executor/process/result.py index 52a9abadc4b..b9e54df9dc1 100644 --- a/v2/ansible/executor/process/result.py +++ b/v2/ansible/executor/process/result.py @@ -33,7 +33,6 @@ try: except ImportError: HAS_ATFORK=False -from ansible.executor.task_result import TaskResult from ansible.playbook.handler import Handler from ansible.playbook.task import Task @@ -130,7 +129,7 @@ class ResultProcess(multiprocessing.Process): for notify in result._task.notify: self._send_result(('notify_handler', result._host, notify)) - if 'results' in result._result: + if result._task.loop: # this task had a loop, and has more than one result, so # loop over all of them instead of a single result result_items = result._result['results'] diff --git a/v2/ansible/executor/task_executor.py b/v2/ansible/executor/task_executor.py index b3080ea9307..7c4cd9cdd85 100644 --- a/v2/ansible/executor/task_executor.py +++ b/v2/ansible/executor/task_executor.py @@ -112,14 +112,12 @@ class TaskExecutor: results = [] - # FIXME: squash items into a flat list here for those modules - # which support it (yum, apt, etc.) but make it smarter - # than it is today? + # make copies of the job vars and task so we can add the item to + # the variables and re-validate the task with the item variable + task_vars = self._job_vars.copy() + items = self._squash_items(items, task_vars) for item in items: - # make copies of the job vars and task so we can add the item to - # the variables and re-validate the task with the item variable - task_vars = self._job_vars.copy() task_vars['item'] = item try: @@ -143,6 +141,22 @@ class TaskExecutor: return results + def _squash_items(self, items, variables): + ''' + Squash items down to a comma-separated list for certain modules which support it + (typically package management modules). + ''' + + if len(items) > 0 and self._task.action in ('apt', 'yum', 'pkgng', 'zypper'): + final_items = [] + for item in items: + variables['item'] = item + if self._task.evaluate_conditional(variables): + final_items.append(item) + return [",".join(final_items)] + else: + return items + def _execute(self, variables=None): ''' The primary workhorse of the executor system, this runs the task diff --git a/v2/ansible/executor/task_result.py b/v2/ansible/executor/task_result.py index a0a0c6a5ddf..2b760bac003 100644 --- a/v2/ansible/executor/task_result.py +++ b/v2/ansible/executor/task_result.py @@ -55,6 +55,7 @@ class TaskResult: if 'results' in self._result: flag = False for res in self._result.get('results', []): - flag |= res.get(key, False) + if isinstance(res, dict): + flag |= res.get(key, False) else: return self._result.get(key, False) diff --git a/v2/bin/ansible-playbook b/v2/bin/ansible-playbook index 4a60f84c9f3..01af12acaee 100755 --- a/v2/bin/ansible-playbook +++ b/v2/bin/ansible-playbook @@ -17,7 +17,8 @@ from ansible.utils.vars import combine_vars from ansible.vars import VariableManager # Implement an ansible.utils.warning() function later -warning = print +import __builtin__ +warning = getattr(__builtin__, 'print') #---------------------------------------------------------------------------------------------------