Merge pull request #1563 from lllama/devel

Add an "executable" option to the command and shell modules
This commit is contained in:
Michael DeHaan 2012-11-08 16:45:48 -08:00
commit 2d3f7544f6
2 changed files with 23 additions and 2 deletions

19
command
View file

@ -60,6 +60,12 @@ options:
version_added: "0.6" version_added: "0.6"
required: false required: false
default: null default: null
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"
examples: examples:
- code: "command: /sbin/shutdown -t now" - code: "command: /sbin/shutdown -t now"
description: "Example from Ansible Playbooks" description: "Example from Ansible Playbooks"
@ -81,6 +87,7 @@ def main():
shell = module.params['shell'] shell = module.params['shell']
chdir = module.params['chdir'] chdir = module.params['chdir']
executable = module.params['executable']
args = module.params['args'] args = module.params['args']
if args.strip() == '': if args.strip() == '':
@ -94,7 +101,7 @@ def main():
startd = datetime.datetime.now() startd = datetime.datetime.now()
try: try:
cmd = subprocess.Popen(args, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = subprocess.Popen(args, executable=executable, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate() out, err = cmd.communicate()
except (OSError, IOError), e: except (OSError, IOError), e:
module.fail_json(rc=e.errno, msg=str(e), cmd=args) module.fail_json(rc=e.errno, msg=str(e), cmd=args)
@ -140,11 +147,12 @@ class CommandModule(AnsibleModule):
params = {} params = {}
params['chdir'] = None params['chdir'] = None
params['shell'] = False params['shell'] = False
params['executable'] = None
if args.find("#USE_SHELL") != -1: if args.find("#USE_SHELL") != -1:
args = args.replace("#USE_SHELL", "") args = args.replace("#USE_SHELL", "")
params['shell'] = True params['shell'] = True
r = re.compile(r'(^|\s)(creates|removes|chdir)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)') r = re.compile(r'(^|\s)(creates|removes|chdir|executable)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)')
for m in r.finditer(args): for m in r.finditer(args):
v = m.group(4).replace("\\", "") v = m.group(4).replace("\\", "")
if m.group(2) == "creates": if m.group(2) == "creates":
@ -182,6 +190,13 @@ class CommandModule(AnsibleModule):
elif v[0] != '/': elif v[0] != '/':
self.fail_json(rc=259, msg="the path for 'chdir' argument must be fully qualified") self.fail_json(rc=259, msg="the path for 'chdir' argument must be fully qualified")
params['chdir'] = v params['chdir'] = v
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
args = r.sub("", args) args = r.sub("", args)
params['args'] = args params['args'] = args
return (params, params['args']) return (params, params['args'])

6
shell
View file

@ -27,6 +27,12 @@ options:
- cd into this directory before running the command (0.6 and later) - cd into this directory before running the command (0.6 and later)
required: false required: false
default: null default: null
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"
examples: examples:
- code: "shell: somescript.sh >> somelog.txt" - code: "shell: somescript.sh >> somelog.txt"
description: Execute the command in remote shell description: Execute the command in remote shell