def _ansible_tower_list_jobs_function(self, event, *args, **kwargs):
        """Function: get all jobs, using criteria to filter if present"""
        try:
            validate_fields(("url"),
                            self.options)  # validate key app.config settings

            # Get the function parameters:
            tower_job_status_list = self.get_select_param(
                kwargs.get("tower_job_status"))  # multi-select
            tower_last_updated = self.get_select_param(
                kwargs.get("tower_last_updated"))  # select

            log = logging.getLogger(__name__)
            log.info("tower_job_status: %s", tower_job_status_list)
            log.info("tower_last_updated: %s", tower_last_updated)

            last_update_epoch = None
            if tower_last_updated:
                last_update_epoch = convert_job_search_time(tower_last_updated)
                log.debug(last_update_epoch)

            result = ResultPayload(SECTION_HDR, **kwargs)
            rc = RequestsCommon(self.opts, self.options)

            # PUT YOUR FUNCTION IMPLEMENTATION CODE HERE
            yield StatusMessage("starting...")
            job_results = []
            url = "/".join(
                (clean_url(self.options['url']), TOWER_API_BASE, JOBS_URL))
            # common
            basic_auth, cafile = get_common_request_items(self.options)

            # loop if results are paged
            while url:
                paged_results, next_url = get_paged_jobs(
                    rc, url, basic_auth, cafile, tower_job_status_list,
                    last_update_epoch)
                if paged_results:
                    job_results.extend(paged_results)

                if next_url:
                    url = "/".join((clean_url(self.options.get('url')),
                                    next_url)).replace("//api", "/api")
                else:
                    url = None

            result_payload = result.done(True, job_results)
            yield StatusMessage("done...")

            # Produce a FunctionResult with the results
            yield FunctionResult(result_payload)
        except Exception:
            yield FunctionError()
    def _ansible_tower_list_job_templates_function(self, event, *args,
                                                   **kwargs):
        """Function: Run an ansible module outside of the job template"""
        try:
            validate_fields(("url"),
                            self.options)  # validate key app.config settings

            # Get the function parameters:
            tower_hosts = kwargs.get("tower_hosts")  # text
            tower_module = self.get_select_param(
                kwargs.get("tower_module"))  # text
            tower_arguments = kwargs.get("tower_arguments")  # text
            tower_inventory = kwargs.get("tower_inventory")  # number
            tower_credential = kwargs.get("tower_credential")  # number

            log = logging.getLogger(__name__)
            log.info("tower_hosts: %s", tower_hosts)
            log.info("tower_module: %s", tower_module)
            log.info("tower_arguments: %s", tower_arguments)
            log.info("tower_inventory: %s", tower_inventory)
            log.info("tower_credential: %s", tower_credential)

            result = ResultPayload(SECTION_HDR, **kwargs)
            rc = RequestsCommon(self.opts, self.options)

            # PUT YOUR FUNCTION IMPLEMENTATION CODE HERE
            yield StatusMessage("starting...")

            url = "/".join(
                (clean_url(self.options['url']), TOWER_API_BASE, AD_HOC_URL))
            # common
            basic_auth, cafile = get_common_request_items(self.options)

            arguments = {
                "module_name": tower_module,
                "limit": tower_hosts,
                "module_args": tower_arguments,
                "inventory": tower_inventory,
                "credential": tower_credential
            }

            rc = RequestsCommon(self.opts, self.options)
            results = rc.execute_call_v2("post",
                                         url,
                                         auth=basic_auth,
                                         json=arguments,
                                         headers=JSON_HEADERS,
                                         verify=cafile)

            result_payload = result.done(True, results.json())
            yield StatusMessage("done...")

            # Produce a FunctionResult with the results
            yield FunctionResult(result_payload)
        except Exception:
            yield FunctionError()
Ejemplo n.º 3
0
def run_job_template(opts, options, tower_template_id, tower_template_hosts,
                     tower_template_arguments, tower_template_run_tags,
                     tower_template_skip_tags):
    """
    invoke the call to launch a job
    :param opts: full set of app.config settings
    :param options: specific settings for ansible tower
    :param tower_template_id:
    :param tower_teemplate_hosts
    :param tower_template_arguments:
    :param tower_template_run_tags:
    :param tower_template_skip_tags:
    :return: json formatted job launch details
    """
    log = logging.getLogger(__name__)
    rc = RequestsCommon(opts, options)

    url = "/".join((clean_url(options['url']), TOWER_API_BASE,
                    LAUNCH_URL.format(id=tower_template_id)))

    arguments = {}
    if tower_template_arguments:
        arguments['extra_vars'] = make_extra_vars(tower_template_arguments)

    if tower_template_run_tags:
        arguments['job_tags'] = tower_template_run_tags

    if tower_template_skip_tags:
        arguments['skip_tags'] = tower_template_skip_tags

    if tower_template_hosts:
        arguments['limit'] = tower_template_hosts

    log.debug("Arguments: %s", arguments)

    # common
    basic_auth, cafile = get_common_request_items(options)
    tower_result = rc.execute_call_v2("post",
                                      url,
                                      auth=basic_auth,
                                      json=arguments,
                                      headers=JSON_HEADERS,
                                      verify=cafile)
    return tower_result.json()
Ejemplo n.º 4
0
def selftest_function(opts):
    """
    Placeholder for selftest function. An example use would be to test package api connectivity.
    Suggested return values are be unimplemented, success, or failure.
    """
    options = opts.get(SECTION_HDR, {})

    rc = RequestsCommon(opts, options)

    # common
    basic_auth, cafile = get_common_request_items(options)

    # get summary information
    ping_url = "/".join((clean_url(options['url']), TOWER_API_BASE, PING_URL))

    try:
        ping_result = rc.execute_call_v2("get", ping_url, proxies=rc.get_proxies(), auth=basic_auth,
                                         verify=cafile)

        return {"state": "success"}
    except Exception as err:
        return {"state": "failure", "reason": str(err)}
Ejemplo n.º 5
0
    def _ansible_tower_get_job_results_function(self, event, *args, **kwargs):
        """Function: Get the results of a complete job"""
        try:
            validate_fields(("url"),
                            self.options)  # validate key app.config settings

            # Get the function parameters:
            tower_job_id = kwargs.get("tower_job_id")  # text
            tower_save_as = self.get_select_param(
                kwargs.get("tower_save_as"))  # select
            incident_id = kwargs.get("incident_id")  # number

            log = logging.getLogger(__name__)
            log.info("tower_job_id: %s", tower_job_id)
            log.info("tower_save_as: %s", tower_save_as)
            log.info("incident_id: %s", incident_id)

            result = ResultPayload(SECTION_HDR, **kwargs)
            rc = RequestsCommon(self.opts, self.options)

            # PUT YOUR FUNCTION IMPLEMENTATION CODE HERE
            yield StatusMessage("starting...")

            # common
            basic_auth, cafile = get_common_request_items(self.options)

            # get summary information
            summary_url = "/".join(
                (clean_url(self.options['url']), TOWER_API_BASE,
                 JOBS_URL.format(id=tower_job_id)))
            summary_result = rc.execute_call_v2("get",
                                                summary_url,
                                                proxies=rc.get_proxies(),
                                                auth=basic_auth,
                                                verify=cafile)
            json_summary = summary_result.json()

            event_url = "/".join(
                (clean_url(self.options['url']), TOWER_API_BASE,
                 EVENTS_URL.format(id=tower_job_id)))

            events_result = rc.execute_call_v2("get",
                                               event_url,
                                               proxies=rc.get_proxies(),
                                               auth=basic_auth,
                                               verify=cafile)

            json_events = events_result.json()

            payload = {"summary": json_summary, "events": json_events}

            # save results as attachment will return no results.content
            if tower_save_as == "attachment":
                res_client = self.rest_client()
                save_as_attachment(res_client, incident_id, payload)

            result_payload = result.done(True, payload)

            yield StatusMessage("done...")

            # Produce a FunctionResult with the results
            yield FunctionResult(result_payload)
        except Exception:
            yield FunctionError()