Added 'script', a module that can push and insantly run a remote script. Replaces the need to copy+command.

This is a bit of a rough draft, some error handling still needs to be added.
This commit is contained in:
Michael DeHaan 2012-10-20 12:12:07 -04:00
parent 7e2d6c0050
commit 4104478abd
5 changed files with 98 additions and 8 deletions

View file

@ -29,6 +29,7 @@ import optparse
import time
import datetime
import subprocess
import traceback
# modules that are ok that they do not have documentation strings
BLACKLIST_MODULES = [
@ -149,11 +150,8 @@ def get_docstring(filename, verbose=False):
doc = yaml.load(child.value.s)
except:
if verbose:
raise
else:
print "unable to parse %s" % filename
traceback.print_exc()
print "unable to parse %s" % filename
return doc

View file

@ -0,0 +1,67 @@
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import pwd
import traceback
import shlex
import ansible.constants as C
from ansible import utils
from ansible import errors
from ansible import module_common
from ansible.runner.return_data import ReturnData
class ActionModule(object):
def __init__(self, runner):
self.runner = runner
def run(self, conn, tmp, module_name, module_args, inject):
''' handler for file transfer operations '''
# load up options
options = utils.parse_kv(module_args)
tokens = shlex.split(module_args)
source = tokens[0]
# FIXME: error handling
args = " ".join(tokens[1:])
source = utils.template(self.runner.basedir, source, inject)
source = utils.path_dwim(self.runner.basedir, source)
exec_rc = None
# transfer the file to a remote tmp location
source = source.replace('\x00','') # why does this happen here?
args = args.replace('\x00','') # why does this happen here?
tmp_src = os.path.join(tmp, os.path.basename(source))
tmp_src = tmp_src.replace('\x00', '')
conn.put_file(source, tmp_src)
# fix file permissions when the copy is done as a different user
if self.runner.sudo and self.runner.sudo_user != 'root':
self.runner._low_level_exec_command(conn, "chmod a+r %s" % tmp_src, tmp)
# make executable
self.runner._low_level_exec_command(conn, "chmod +x %s" % tmp_src, tmp)
# run it through the command module
module_args = tmp_src + " " + args + " #USE_SHELL"
return self.runner._execute_module(conn, tmp, 'command', module_args, inject=inject)

View file

@ -184,6 +184,8 @@ def json_loads(data):
def parse_json(raw_data):
''' this version for module return data only '''
orig_data = raw_data
# ignore stuff like tcgetattr spewage or other warnings
data = filter_leading_non_json_lines(raw_data)
@ -202,7 +204,7 @@ def parse_json(raw_data):
for t in tokens:
if t.find("=") == -1:
raise errors.AnsibleError("failed to parse: %s" % raw_data)
raise errors.AnsibleError("failed to parse: %s" % orig_data)
(key,value) = t.split("=", 1)
if key == 'changed' or 'failed':
if value.lower() in [ 'true', '1' ]:
@ -213,7 +215,7 @@ def parse_json(raw_data):
value = int(value)
results[key] = value
if len(results.keys()) == 0:
return { "failed" : True, "parsed" : False, "msg" : raw_data }
return { "failed" : True, "parsed" : False, "msg" : orig_data }
return results
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")

View file

@ -94,7 +94,7 @@ examples:
- code: postgresql_user db=acme user=django password=ceec4eif7ya priv=CONNECT/products:ALL
description: Create django user and grant access to database and products table
- code: postgresql_user user=rails password=secret role_attr_flags=CREATEDB,NOSUPERUSER
- description: Create rails user, grant privilege to create other databases and demote rails from super user status
description: Create rails user, grant privilege to create other databases and demote rails from super user status
- code: postgresql_user db=acme user=test priv=ALL/products:ALL state=absent fail_on_user=no
description: Remove test user privileges from acme
- code: postgresql_user db=test user=test priv=ALL state=absent

23
library/script Executable file
View file

@ -0,0 +1,23 @@
DOCUMENTATION = """
---
module: script
short_description: Runs a local script on a remote node
description:
- The command module takes the script name followed by a list of space-delimited arguments.
- The given command will be processed through the shell environment.
options:
free_form:
description:
- the command module takes a free form command to run
required: true
default: null
aliases: []
examples:
- description: "Example from Ansible Playbooks"
code: "script: /some/local/script.sh --some-arguments 1234"
notes:
- It is preferable to write Ansible modules than pushing scripts. Convert your script to an ansible module for bonus points!
author: Michael DeHaan
"""