Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active April 14, 2016 15:36
Show Gist options
  • Select an option

  • Save sivel/b00ad7c658599d6c308cf269f6d40d90 to your computer and use it in GitHub Desktop.

Select an option

Save sivel/b00ad7c658599d6c308cf269f6d40d90 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) 2016, Matt Martz <matt@sivel.net>
#
# 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/>.
from __future__ import (absolute_import, division, print_function)
import json
import os
import shlex
import subprocess
import sys
from ansible import constants
from ansible.cli import CLI
from ansible.parsing.dataloader import DataLoader
from ansible.parsing.splitter import parse_kv
from ansible.playbook.play_context import PlayContext, display
from ansible.playbook.task import Task
from ansible.plugins import action_loader, connection_loader, module_loader
from ansible.plugins.strategy import SharedPluginLoaderObj
from ansible.template import Templar
__metaclass__ = type
def parse():
parser = CLI.base_parser(
usage='%prog /path/to/module [options]',
runas_opts=False,
inventory_opts=False,
async_opts=False,
output_opts=False,
connect_opts=False,
check_opts=False,
runtask_opts=False,
vault_opts=False,
fork_opts=False,
module_opts=False,
)
parser.add_option('-a', '--args', dest='module_args',
help="module arguments", default='')
parser.add_option('--explode', help='explode the module after execution',
action='store_true')
options, args = parser.parse_args()
if len(args) != 1:
parser.error("Missing path to module to execute")
return options, args
def main():
options, args = parse()
dirname = os.path.dirname(args[0])
basename, _ = os.path.splitext(os.path.basename(args[0]))
module_loader.add_directory(dirname)
constants.DEFAULT_KEEP_REMOTE_FILES = True
loader = DataLoader()
if options.module_args.startswith('@'):
module_args = loader.load_from_file(options.module_args[1:])
elif options.module_args.startswith('{'):
module_args = loader.load(options.module_args)
else:
module_args = parse_kv(options.module_args)
task = Task.load({
'action': {
'module': basename,
'args': module_args
}
})
play_context = PlayContext()
display.verbosity = 4
connection = connection_loader.get('local', play_context, sys.stdin)
templar = Templar(loader=loader, variables={})
shared_loader_obj = SharedPluginLoaderObj()
action = action_loader.get('normal', task, connection, play_context,
loader, templar, shared_loader_obj)
tmp = action._make_tmp_path(None)
out = action.run(tmp=tmp)
print('\n%s' % json.dumps(out, sort_keys=True, indent=4))
print('\nCompiled module is located at %s' % tmp)
if options.explode:
explode_command = '%s %s/%s explode' % (sys.executable,
tmp.rstrip('/'), basename)
p = subprocess.Popen(shlex.split(explode_command))
p.communicate()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment