1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add options to control output and execution of test-module

test-module is useful but sometimes you want to edit the
result before running it to e.g. set a debug point.

Added a noexecute option (i.e. just create the module script, don't
run it) and an output option to choose the filename of the result.
This commit is contained in:
Will Thames 2014-09-09 19:06:06 +10:00 committed by Abhijit Menon-Sen
parent 8729543cee
commit b05485d4b3

View file

@ -27,6 +27,7 @@
# test-module -m ../library/system/service -a "name=httpd ensure=restarted" # test-module -m ../library/system/service -a "name=httpd ensure=restarted"
# test-module -m ../library/system/service -a "name=httpd ensure=restarted" --debugger /usr/bin/pdb # test-module -m ../library/system/service -a "name=httpd ensure=restarted" --debugger /usr/bin/pdb
# test-modulr -m ../library/file/lineinfile -a "dest=/etc/exports line='/srv/home hostname1(rw,sync)'" --check # test-modulr -m ../library/file/lineinfile -a "dest=/etc/exports line='/srv/home hostname1(rw,sync)'" --check
# test-module -m ../library/commands/command -a "echo hello" -n -o "test_hello"
import sys import sys
import base64 import base64
@ -66,6 +67,11 @@ def parse():
default='python={}'.format(sys.executable)) default='python={}'.format(sys.executable))
parser.add_option('-c', '--check', dest='check', action='store_true', parser.add_option('-c', '--check', dest='check', action='store_true',
help="run the module in check mode") help="run the module in check mode")
parser.add_option('-n', '--noexecute', dest='execute', action='store_false',
default=True, help="do not run the resulting module")
parser.add_option('-o', '--output', dest='filename',
help="Filename for resulting module",
default="~/.ansible_module_generated")
options, args = parser.parse_args() options, args = parser.parse_args()
if not options.module_path: if not options.module_path:
parser.print_help() parser.print_help()
@ -84,7 +90,7 @@ def write_argsfile(argstring, json=False):
argsfile.close() argsfile.close()
return argspath return argspath
def boilerplate_module(modfile, args, interpreter, check): def boilerplate_module(modfile, args, interpreter, check, destfile):
""" simulate what ansible does with new style modules """ """ simulate what ansible does with new style modules """
#module_fh = open(modfile) #module_fh = open(modfile)
@ -128,7 +134,7 @@ def boilerplate_module(modfile, args, interpreter, check):
inject inject
) )
modfile2_path = os.path.expanduser("~/.ansible_module_generated") modfile2_path = os.path.expanduser(destfile)
print "* including generated source, if any, saving to: %s" % modfile2_path print "* including generated source, if any, saving to: %s" % modfile2_path
print "* this may offset any line numbers in tracebacks/debuggers!" print "* this may offset any line numbers in tracebacks/debuggers!"
modfile2 = open(modfile2_path, 'w') modfile2 = open(modfile2_path, 'w')
@ -178,7 +184,7 @@ def rundebug(debugger, modfile, argspath):
def main(): def main():
options, args = parse() options, args = parse()
(modfile, module_style) = boilerplate_module(options.module_path, options.module_args, options.interpreter, options.check) (modfile, module_style) = boilerplate_module(options.module_path, options.module_args, options.interpreter, options.check, options.filename)
argspath = None argspath = None
if module_style != 'new': if module_style != 'new':
@ -188,6 +194,7 @@ def main():
argspath = write_argsfile(options.module_args, json=False) argspath = write_argsfile(options.module_args, json=False)
else: else:
raise Exception("internal error, unexpected module style: %s" % module_style) raise Exception("internal error, unexpected module style: %s" % module_style)
if options.execute:
if options.debugger: if options.debugger:
rundebug(options.debugger, modfile, argspath) rundebug(options.debugger, modfile, argspath)
else: else: