From ecb944892dfda56cfca4c22ad00ca958b543b55f Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 26 May 2012 01:20:53 -0400 Subject: [PATCH] Tweaks on previous refactoring of playbook, version bump a 0.4 reference, remove some debug, etc --- lib/ansible/__init__.py | 2 +- lib/ansible/playbook/play.py | 23 ++++++++++++++--------- lib/ansible/playbook/task.py | 21 ++++++++------------- lib/ansible/runner/__init__.py | 3 +++ lib/ansible/runner/connection.py | 4 +--- library/virt | 1 - 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/ansible/__init__.py b/lib/ansible/__init__.py index f927a1fd189..7e8cc39d360 100644 --- a/lib/ansible/__init__.py +++ b/lib/ansible/__init__.py @@ -14,5 +14,5 @@ # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -__version__ = '0.4' +__version__ = '0.5' __author__ = 'Michael DeHaan' diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 7795038f96a..4ad9e7d9306 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -33,6 +33,9 @@ class Play(object): # ************************************************* def __init__(self, playbook, ds): + ''' constructor loads from a play datastructure ''' + + # TODO: more error handling self._ds = ds self.playbook = playbook @@ -90,16 +93,20 @@ class Play(object): # ************************************************* - def handlers(self): - return self._handlers - def tasks(self): + ''' return task objects for this play ''' return self._tasks + def handlers(self): + ''' return handler objects for this play ''' + return self._handlers + # ************************************************* def _get_vars(self, dirname): - ''' load the vars section from a play ''' + ''' load the vars section from a play, accounting for all sorts of variable features + including loading from yaml files, prompting, and conditional includes of the first + file found in a list. ''' if self.vars is None: self.vars = {} @@ -120,8 +127,7 @@ class Play(object): if type(self.vars_prompt) != dict: raise errors.AnsibleError("'vars_prompt' section must contain only key/value pairs") for vname in self.vars_prompt: - # TODO: make this prompt one line and consider double entry - vars[vname] = self.callbacks.on_vars_prompt(vname) + vars[vname] = self.playbook.callbacks.on_vars_prompt(vname) results = self.playbook.extra_vars.copy() results.update(vars) @@ -132,18 +138,17 @@ class Play(object): def update_vars_files(self, hosts): ''' calculate vars_files, which requires that setup runs first so ansible facts can be mixed in ''' for h in hosts: - self.update_vars_files_for_host(h) + self._update_vars_files_for_host(h) # ************************************************* - def update_vars_files_for_host(self, host): + def _update_vars_files_for_host(self, host): if not host in self.playbook.SETUP_CACHE: # no need to process failed hosts or hosts not in this play return for filename in self.vars_files: - # TODO: maybe have to template the path here... if type(filename) == list: diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 9983d13ab14..a3d94c6bd77 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -17,11 +17,7 @@ ############################################# -#import ansible.inventory -#import ansible.runner -#import ansible.constants as C -#from ansible import utils -#from ansible import errors +from ansible import errors class Task(object): @@ -31,14 +27,13 @@ class Task(object): ] def __init__(self, play, ds): + ''' constructor loads from a task or handler datastructure ''' - self.play = play + # TODO: more error handling - # FIXME: error handling on invalid fields - # action... - - self.name = ds.get('name', None) - self.action = ds.get('action', '') + self.play = play + self.name = ds.get('name', None) + self.action = ds.get('action', '') self.notified_by = [] if self.name is None: @@ -53,8 +48,7 @@ class Task(object): tokens = self.action.split(None, 1) if len(tokens) < 1: - # FIXME: better error handling - raise Exception("invalid action in task: %s" % ds) + raise errors.AnsibleError("invalid/missing action in task") self.module_name = tokens[0] self.module_args = '' @@ -63,6 +57,7 @@ class Task(object): # include task specific vars self.module_vars = ds.get('vars', {}) + if 'first_available_file' in ds: self.module_vars['first_available_file'] = ds.get('first_available_file') diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index a263544944d..378fee3f63d 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -549,6 +549,9 @@ class Runner(object): return ReturnData(host=conn.host, comm_ok=False, result=result) + if self.module_vars is not None: + inject.update(self.module_vars) + source = utils.template(source, inject, self.setup_cache) #(host, ok, data, err) = (None, None, None, None) diff --git a/lib/ansible/runner/connection.py b/lib/ansible/runner/connection.py index b5d44f06234..63f59794e73 100644 --- a/lib/ansible/runner/connection.py +++ b/lib/ansible/runner/connection.py @@ -42,15 +42,13 @@ with warnings.catch_warnings(): class Connection(object): ''' Handles abstract connections to remote hosts ''' - _LOCALHOSTRE = re.compile(r"^(127.0.0.1|localhost|%s)$" % os.uname()[1]) - def __init__(self, runner, transport,sudo_user): self.runner = runner self.transport = transport self.sudo_user = sudo_user def connect(self, host, port=None): conn = None - if self.transport == 'local' and self._LOCALHOSTRE.search(host): + if self.transport == 'local': conn = LocalConnection(self.runner, host) elif self.transport == 'paramiko': conn = ParamikoConnection(self.runner, host, port) diff --git a/library/virt b/library/virt index ac048bab2d3..ffa57c76b9b 100755 --- a/library/virt +++ b/library/virt @@ -124,7 +124,6 @@ class LibvirtConnection(object): def get_status2(self, vm): state = vm.info()[0] - # print "DEBUG: state: %s" % state return VIRT_STATE_NAME_MAP.get(state,"unknown") def get_status(self, vmid):