#!/usr/bin/python ''' Ansible action plugin for configuring Wildfly ''' from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible.plugins.action import ActionBase from ansible.utils.display import Display display = Display() import json from six import string_types class ActionModule(ActionBase): def run(self, tmp=None, task_vars=None): super(ActionModule, self).run(tmp, task_vars) # Define support for check mode and async self._supports_check_mode = True self._supports_async = False # module_args = self._task.args.copy() # module_return = self._execute_module(module_name='setup', # module_args=module_args, # task_vars=task_vars, tmp=tmp) # ret = dict() # remote_date = None # if not module_return.get('failed'): # for key, value in module_return['ansible_facts'].items(): # if key == 'ansible_date_time': # remote_date = value['iso8601'] # if remote_date: # remote_date_obj = datetime.strptime(remote_date, '%Y-%m-%dT%H:%M:%SZ') # time_delta = datetime.now() - remote_date_obj # ret['delta_seconds'] = time_delta.seconds # ret['delta_days'] = time_delta.days # ret['delta_microseconds'] = time_delta.microseconds # return dict(ansible_facts=dict(ret)) task_args = self._task.args.copy() check_mode = self._play_context.check_mode cli_command, config, dry_run = read_args(task_args, check_mode) # read current config # if not self.prev: cur_result = self.execute_command( '{} --output-json --command="/:read-resource(recursive)"'.format( cli_command), check_mode_unsafe=False ) if cur_result.get('failed'): return cur_result try: cur_state = json.loads(cur_result.get('stdout')).get('result') except Exception as e: return merge_hash(cur_result, dict( failed=True, msg="Error parsing JSON from Wildfly CLI: {}".format(str(e)) )) return cur_result def execute_command(self, command, stdin=None, check_mode_unsafe=True): ''' Execute command module and return result ''' return self._execute_module( module_name='command', module_args=dict( _raw_params=command, stdin=stdin, _ansible_check_mode=(self._play_context.check_mode and check_mode_unsafe) ) ) def read_args(task_args, check_mode): ''' Read and validate invocation arguments ''' # CLI command for connecting to Wildfly admin interface cli_command = task_args.get('cli_cmd', 'jboss-cli.sh --connect') # desired configuration config = task_args.get('config', False) if not config and task_args.get('config_list', False): config = task_args.get('config_list') display.deprecated('"config_list" argument should not be used. ' 'Please use "config" argument instead.') # validate config argument if type(config) is list: pass elif type(config) is dict: config = [dict( config=config, root=task_args.get('root'), state=task_args.get('state', 'present') )] else: raise Exception("config argument should be list or dict") # dry run argument dry_run = (task_args.get('dry_run', False) or check_mode) return cli_command, config, dry_run