|
|
|
@ -16,6 +16,7 @@
|
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import shlex
|
|
|
|
|
|
|
|
|
|
import ansible.constants as C
|
|
|
|
@ -38,12 +39,60 @@ class ActionModule(object):
|
|
|
|
|
# in check mode, always skip this module
|
|
|
|
|
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
|
|
|
|
|
|
|
|
|
|
# extract ansible reserved parameters
|
|
|
|
|
# From library/command keep in sync
|
|
|
|
|
creates = None
|
|
|
|
|
removes = None
|
|
|
|
|
r = re.compile(r'(^|\s)(creates|removes)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)')
|
|
|
|
|
for m in r.finditer(module_args):
|
|
|
|
|
v = m.group(4).replace("\\", "")
|
|
|
|
|
if m.group(2) == "creates":
|
|
|
|
|
creates = v
|
|
|
|
|
elif m.group(2) == "removes":
|
|
|
|
|
removes = v
|
|
|
|
|
module_args = r.sub("", module_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if creates:
|
|
|
|
|
# do not run the command if the line contains creates=filename
|
|
|
|
|
# and the filename already exists. This allows idempotence
|
|
|
|
|
# of command executions.
|
|
|
|
|
module_args_tmp = "path=%s" % creates
|
|
|
|
|
module_return = self.runner._execute_module(conn, tmp, 'stat', module_args_tmp, inject=inject, complex_args=complex_args, persist_files=True)
|
|
|
|
|
stat = module_return.result.get('stat', None)
|
|
|
|
|
if stat and stat.get('exists', False):
|
|
|
|
|
return ReturnData(
|
|
|
|
|
conn=conn,
|
|
|
|
|
comm_ok=True,
|
|
|
|
|
result=dict(
|
|
|
|
|
skipped=True,
|
|
|
|
|
msg=("skipped, since %s exists" % creates)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
|
module_args_tmp = "path=%s" % removes
|
|
|
|
|
module_return = self.runner._execute_module(conn, tmp, 'stat', module_args_tmp, inject=inject, complex_args=complex_args, persist_files=True)
|
|
|
|
|
stat = module_return.result.get('stat', None)
|
|
|
|
|
if stat and not stat.get('exists', False):
|
|
|
|
|
return ReturnData(
|
|
|
|
|
conn=conn,
|
|
|
|
|
comm_ok=True,
|
|
|
|
|
result=dict(
|
|
|
|
|
skipped=True,
|
|
|
|
|
msg=("skipped, since %s does not exist" % removes)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Decode the result of shlex.split() to UTF8 to get around a bug in that's been fixed in Python 2.7 but not Python 2.6.
|
|
|
|
|
# See: http://bugs.python.org/issue6988
|
|
|
|
|
tokens = shlex.split(module_args.encode('utf8'))
|
|
|
|
|
tokens = [s.decode('utf8') for s in tokens]
|
|
|
|
|
|
|
|
|
|
# extract source script
|
|
|
|
|
source = tokens[0]
|
|
|
|
|
|
|
|
|
|
# FIXME: error handling
|
|
|
|
|
args = " ".join(tokens[1:])
|
|
|
|
|
source = template.template(self.runner.basedir, source, inject)
|
|
|
|
|