mirror of https://github.com/ansible/ansible.git
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.pull/1648/head
parent
7e2d6c0050
commit
4104478abd
@ -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)
|
||||
|
@ -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
|
||||
"""
|
||||
|
Loading…
Reference in New Issue