mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
6.9 KiB
Plaintext
208 lines
6.9 KiB
Plaintext
13 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
13 years ago
|
|
||
13 years ago
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
|
||
|
#
|
||
|
# 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/>.
|
||
13 years ago
|
|
||
13 years ago
|
import sys
|
||
|
import datetime
|
||
13 years ago
|
import traceback
|
||
12 years ago
|
import re
|
||
13 years ago
|
import shlex
|
||
|
import os
|
||
13 years ago
|
|
||
12 years ago
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: command
|
||
|
short_description: Executes a command on a remote node
|
||
|
description:
|
||
12 years ago
|
- The M(command) module takes the command name followed by a list of space-delimited arguments.
|
||
12 years ago
|
- The given command will be executed on all selected nodes. It will not be
|
||
|
processed through the shell, so variables like C($HOME) and operations
|
||
|
like C("<"), C(">"), C("|"), and C("&") will not work. As such, all
|
||
|
paths to commands must be fully qualified
|
||
|
options:
|
||
|
free_form:
|
||
|
description:
|
||
|
- the command module takes a free form command to run
|
||
|
required: true
|
||
|
default: null
|
||
|
aliases: []
|
||
|
creates:
|
||
|
description:
|
||
|
- a filename, when it already exists, this step will B(not) be run.
|
||
|
required: no
|
||
|
default: null
|
||
12 years ago
|
removes:
|
||
|
description:
|
||
|
- a filename, when it does not exist, this step will B(not) be run.
|
||
|
version_added: "0.8"
|
||
|
required: no
|
||
|
default: null
|
||
12 years ago
|
chdir:
|
||
|
description:
|
||
|
- cd into this directory before running the command
|
||
|
version_added: "0.6"
|
||
|
required: false
|
||
|
default: null
|
||
12 years ago
|
executable:
|
||
|
description:
|
||
|
- change the shell used to execute the command. Should be an absolute path to the executable.
|
||
|
required: false
|
||
|
default: null
|
||
|
version_added: "0.9"
|
||
12 years ago
|
examples:
|
||
12 years ago
|
- code: "command: /sbin/shutdown -t now"
|
||
12 years ago
|
description: "Example from Ansible Playbooks"
|
||
12 years ago
|
- code: "command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database"
|
||
12 years ago
|
description: "C(creates), C(removes), and C(chdir) can be specified after the command. For instance, if you only want to run a command if a certain file does not exist, use this."
|
||
12 years ago
|
notes:
|
||
|
- If you want to run a command through the shell (say you are using C(<),
|
||
|
C(>), C(|), etc), you actually want the M(shell) module instead. The
|
||
|
M(command) module is much more secure as it's not affected by the user's
|
||
|
environment.
|
||
|
author: Michael DeHaan
|
||
|
'''
|
||
|
|
||
13 years ago
|
def main():
|
||
|
|
||
|
# the command module is the one ansible module that does not take key=value args
|
||
|
# hence don't copy this one if you are looking to build others!
|
||
|
module = CommandModule(argument_spec=dict())
|
||
|
|
||
|
shell = module.params['shell']
|
||
13 years ago
|
chdir = module.params['chdir']
|
||
12 years ago
|
executable = module.params['executable']
|
||
13 years ago
|
args = module.params['args']
|
||
12 years ago
|
creates = module.params['creates']
|
||
|
removes = module.params['removes']
|
||
13 years ago
|
|
||
12 years ago
|
if args.strip() == '':
|
||
12 years ago
|
module.fail_json(rc=256, msg="no command given")
|
||
12 years ago
|
|
||
13 years ago
|
if chdir:
|
||
12 years ago
|
os.chdir(os.path.expanduser(chdir))
|
||
13 years ago
|
|
||
12 years ago
|
if creates:
|
||
|
# do not run the command if the line contains creates=filename
|
||
|
# and the filename already exists. This allows idempotence
|
||
|
# of command executions.
|
||
|
v = os.path.expanduser(creates)
|
||
|
if os.path.exists(v):
|
||
|
module.exit_json(
|
||
|
cmd=args,
|
||
|
stdout="skipped, since %s exists" % v,
|
||
|
skipped=True,
|
||
|
changed=False,
|
||
|
stderr=False,
|
||
|
rc=0
|
||
|
)
|
||
|
|
||
|
if removes:
|
||
|
# do not run the command if the line contains removes=filename
|
||
|
# and the filename does not exist. This allows idempotence
|
||
|
# of command executions.
|
||
|
v = os.path.expanduser(removes)
|
||
|
if not os.path.exists(v):
|
||
|
module.exit_json(
|
||
|
cmd=args,
|
||
|
stdout="skipped, since %s does not exist" % v,
|
||
|
skipped=True,
|
||
|
changed=False,
|
||
|
stderr=False,
|
||
|
rc=0
|
||
|
)
|
||
|
|
||
13 years ago
|
if not shell:
|
||
|
args = shlex.split(args)
|
||
|
startd = datetime.datetime.now()
|
||
|
|
||
12 years ago
|
rc, out, err = module.run_command(args, executable=executable)
|
||
13 years ago
|
|
||
|
endd = datetime.datetime.now()
|
||
|
delta = endd - startd
|
||
|
|
||
|
if out is None:
|
||
12 years ago
|
out = ''
|
||
13 years ago
|
if err is None:
|
||
12 years ago
|
err = ''
|
||
13 years ago
|
|
||
|
module.exit_json(
|
||
|
cmd = args,
|
||
12 years ago
|
stdout = out.rstrip("\r\n"),
|
||
|
stderr = err.rstrip("\r\n"),
|
||
12 years ago
|
rc = rc,
|
||
13 years ago
|
start = str(startd),
|
||
|
end = str(endd),
|
||
|
delta = str(delta),
|
||
|
changed = True
|
||
|
)
|
||
|
|
||
|
# include magic from lib/ansible/module_common.py
|
||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||
|
|
||
|
# only the command module should ever need to do this
|
||
|
# everything else should be simple key=value
|
||
|
|
||
|
class CommandModule(AnsibleModule):
|
||
|
|
||
13 years ago
|
def _handle_aliases(self):
|
||
12 years ago
|
return {}
|
||
13 years ago
|
|
||
|
def _check_invalid_arguments(self):
|
||
|
pass
|
||
|
|
||
13 years ago
|
def _load_params(self):
|
||
|
''' read the input and return a dictionary and the arguments string '''
|
||
12 years ago
|
args = MODULE_ARGS
|
||
13 years ago
|
params = {}
|
||
13 years ago
|
params['chdir'] = None
|
||
12 years ago
|
params['creates'] = None
|
||
|
params['removes'] = None
|
||
13 years ago
|
params['shell'] = False
|
||
12 years ago
|
params['executable'] = None
|
||
13 years ago
|
if args.find("#USE_SHELL") != -1:
|
||
12 years ago
|
args = args.replace("#USE_SHELL", "")
|
||
|
params['shell'] = True
|
||
13 years ago
|
|
||
12 years ago
|
r = re.compile(r'(^|\s)(creates|removes|chdir|executable)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)')
|
||
12 years ago
|
for m in r.finditer(args):
|
||
|
v = m.group(4).replace("\\", "")
|
||
|
if m.group(2) == "creates":
|
||
12 years ago
|
params['creates'] = v
|
||
12 years ago
|
elif m.group(2) == "removes":
|
||
12 years ago
|
params['removes'] = v
|
||
12 years ago
|
elif m.group(2) == "chdir":
|
||
12 years ago
|
v = os.path.expanduser(v)
|
||
13 years ago
|
if not (os.path.exists(v) and os.path.isdir(v)):
|
||
12 years ago
|
self.fail_json(rc=258, msg="cannot change to directory '%s': path does not exist" % v)
|
||
12 years ago
|
elif v[0] != os.sep:
|
||
12 years ago
|
self.fail_json(rc=259, msg="the path for 'chdir' argument must be fully qualified")
|
||
13 years ago
|
params['chdir'] = v
|
||
12 years ago
|
elif m.group(2) == "executable":
|
||
|
v = os.path.expanduser(v)
|
||
|
if not (os.path.exists(v)):
|
||
|
self.fail_json(rc=258, msg="cannot use executable '%s': file does not exist" % v)
|
||
|
elif v[0] != '/':
|
||
|
self.fail_json(rc=259, msg="the path for 'executable' argument must be fully qualified")
|
||
|
params['executable'] = v
|
||
12 years ago
|
args = r.sub("", args)
|
||
|
params['args'] = args
|
||
12 years ago
|
return (params, params['args'])
|
||
13 years ago
|
|
||
|
main()
|