Add an "executable" option to the command and shell modules

The option will be passed to the Popen object created and will be used to
execute the command instead of the default shell.
pull/1648/head
Felix Ingram 12 years ago
parent 36c1b4be0e
commit e0f0e8c023

@ -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'])

@ -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

Loading…
Cancel
Save