Server IP : 85.214.239.14 / Your IP : 18.226.170.52 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /lib/python3/dist-packages/ansible_collections/community/general/plugins/module_utils/ |
Upload File : |
# -*- coding: utf-8 -*- # Copyright (c) 2021, Phillipe Smith <phsmithcc@gmail.com> # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) # SPDX-License-Identifier: GPL-3.0-or-later from __future__ import absolute_import, division, print_function __metaclass__ = type import json from ansible.module_utils.urls import fetch_url, url_argument_spec from ansible.module_utils.common.text.converters import to_native def api_argument_spec(): ''' Creates an argument spec that can be used with any module that will be requesting content via Rundeck API ''' api_argument_spec = url_argument_spec() api_argument_spec.update(dict( url=dict(required=True, type="str"), api_version=dict(type="int", default=39), api_token=dict(required=True, type="str", no_log=True) )) return api_argument_spec def api_request(module, endpoint, data=None, method="GET"): """Manages Rundeck API requests via HTTP(S) :arg module: The AnsibleModule (used to get url, api_version, api_token, etc). :arg endpoint: The API endpoint to be used. :kwarg data: The data to be sent (in case of POST/PUT). :kwarg method: "POST", "PUT", etc. :returns: A tuple of (**response**, **info**). Use ``response.read()`` to read the data. The **info** contains the 'status' and other meta data. When a HttpError (status >= 400) occurred then ``info['body']`` contains the error response data:: Example:: data={...} resp, info = fetch_url(module, "http://rundeck.example.org", data=module.jsonify(data), method="POST") status_code = info["status"] body = resp.read() if status_code >= 400 : body = info['body'] """ response, info = fetch_url( module=module, url="%s/api/%s/%s" % ( module.params["url"], module.params["api_version"], endpoint ), data=json.dumps(data), method=method, headers={ "Content-Type": "application/json", "Accept": "application/json", "X-Rundeck-Auth-Token": module.params["api_token"] } ) if info["status"] == 403: module.fail_json(msg="Token authorization failed", execution_info=json.loads(info["body"])) if info["status"] == 409: module.fail_json(msg="Job executions limit reached", execution_info=json.loads(info["body"])) elif info["status"] >= 500: module.fail_json(msg="Rundeck API error", execution_info=json.loads(info["body"])) try: content = response.read() if not content: return None, info else: json_response = json.loads(content) return json_response, info except AttributeError as error: module.fail_json( msg="Rundeck API request error", exception=to_native(error), execution_info=info ) except ValueError as error: module.fail_json( msg="No valid JSON response", exception=to_native(error), execution_info=content )