From 5d6b0280d5e6d1ecd0dd14b0af2a3c5b40533eb6 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 24 Feb 2012 03:45:36 -0500 Subject: [PATCH] Added stub for template execution, WIP. --- examples/playbook.yml | 4 ++++ lib/ansible/runner.py | 22 ++++++++++++++++++++-- library/template | 3 +++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 library/template diff --git a/examples/playbook.yml b/examples/playbook.yml index 0866dd2392..1c73f8269d 100644 --- a/examples/playbook.yml +++ b/examples/playbook.yml @@ -4,6 +4,10 @@ - configure template & module variables - setup - [ "a=2", "b=3", "c=4" ] + - do: + - template from local file template.j2 to remote location /srv/file.out + - template + - [ '/srv/template.j2', '/srv/file.out' ] - do: - update apache - command diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index ed0d184dd0..fa046c0eed 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -111,7 +111,7 @@ class Runner(object): if not ok: return [ host, False, conn ] - if self.module_name != "copy": + if self.module_name not in [ 'copy', 'template' ]: # transfer a module, set it executable, and run it outpath = self._copy_module(conn) self._exec_command(conn, "chmod +x %s" % outpath) @@ -120,7 +120,7 @@ class Runner(object): self._exec_command(conn, "rm -f %s" % outpath) conn.close() return [ host, True, json.loads(result) ] - else: + elif self.module_name == 'copy': # SFTP file copy module is not really a module self.remote_log(conn, 'COPY remote:%s local:%s' % (self.module_args[0], self.module_args[1])) ftp = conn.open_sftp() @@ -128,6 +128,24 @@ class Runner(object): ftp.close() conn.close() return [ host, True, 1 ] + elif self.module_name == 'template': + # template runs COPY then the template module + # TODO: DRY/refactor these + # TODO: things like _copy_module should take the name as a param + tempname = os.path.split(self.module_args[0])[-1] + temppath = self._get_tmp_path(conn, tempname) + self.remote_log(conn, 'COPY remote:%s local:%s' % (self.module_args[0], temppath)) + ftp = conn.open_sftp() + ftp.put(self.module_args[0], temppath) + ftp.close() + self.module_name = 'template' + self.module_args = [ self.module_args[0], temppath ] + outpath = self._copy_module(conn) + self._exec_command(conn, "chmod +x %s" % outpath) + result = self._exec_command(conn, self._command(outpath)) + self._exec_command(conn, "rm -f %s" % outpath) + conn.close() + return [ host, True, json.loads(result) ] def _command(self, outpath): diff --git a/library/template b/library/template new file mode 100644 index 0000000000..db68911cea --- /dev/null +++ b/library/template @@ -0,0 +1,3 @@ +#!/usr/bin/python + +print {}