|
| 1 | +# Copyright: (c) 2025, Robin Gierse <robin.gierse@checkmk.com> |
| 2 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 | + |
| 4 | +from __future__ import absolute_import, division, print_function |
| 5 | + |
| 6 | +__metaclass__ = type |
| 7 | + |
| 8 | +DOCUMENTATION = """ |
| 9 | + name: activation |
| 10 | + author: Robin Gierse (@robin-checkmk) |
| 11 | + version_added: "6.8.0" |
| 12 | +
|
| 13 | + short_description: Get the status of a single activation |
| 14 | +
|
| 15 | + description: |
| 16 | + - Returns the status of a single activation |
| 17 | +
|
| 18 | + options: |
| 19 | +
|
| 20 | + _terms: |
| 21 | + description: activation ID to look up |
| 22 | + required: True |
| 23 | +
|
| 24 | + extends_documentation_fragment: [checkmk.general.common_lookup] |
| 25 | +
|
| 26 | + notes: |
| 27 | + - Like all lookups, this runs on the Ansible controller and is unaffected by other keywords such as 'become'. |
| 28 | + If you need to use different permissions, you must change the command or run Ansible as another user. |
| 29 | + - Alternatively, you can use a shell/command task that runs against localhost and registers the result. |
| 30 | + - The directory of the play is used as the current working directory. |
| 31 | + - It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section! |
| 32 | + This is a limitation of Ansible itself. |
| 33 | +""" |
| 34 | + |
| 35 | +EXAMPLES = """ |
| 36 | +- name: "Show activation status" |
| 37 | + ansible.builtin.debug: |
| 38 | + msg: "Activation status is {{ activation }}" |
| 39 | + vars: |
| 40 | + activation: "{{ lookup('checkmk.general.activation', |
| 41 | + my_activation_id |
| 42 | + server_url=http://myserver, |
| 43 | + site=mysite, |
| 44 | + validate_certs=False, |
| 45 | + automation_user=automation_user, |
| 46 | + automation_secret=automation_secret |
| 47 | + )}}" |
| 48 | +
|
| 49 | +- name: "Use variables from inventory." |
| 50 | + ansible.builtin.debug: |
| 51 | + msg: "Activation status is {{ activation }}" |
| 52 | + vars: |
| 53 | + checkmk_var_server_url: "http://myserver/" |
| 54 | + checkmk_var_site: "mysite" |
| 55 | + checkmk_var_automation_user: "myuser" |
| 56 | + checkmk_var_automation_secret: "mysecret" |
| 57 | + checkmk_var_validate_certs: false |
| 58 | + activation: "{{ lookup('checkmk.general.activation', my_activation_id) }}" |
| 59 | +""" |
| 60 | + |
| 61 | +RETURN = """ |
| 62 | + _list: |
| 63 | + description: |
| 64 | + - activation status |
| 65 | + type: list |
| 66 | + elements: str |
| 67 | +""" |
| 68 | + |
| 69 | +import json |
| 70 | + |
| 71 | +from ansible.errors import AnsibleError |
| 72 | +from ansible.plugins.lookup import LookupBase |
| 73 | +from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import ( |
| 74 | + CheckMKLookupAPI, |
| 75 | +) |
| 76 | + |
| 77 | + |
| 78 | +class LookupModule(LookupBase): |
| 79 | + def run(self, terms, variables, **kwargs): |
| 80 | + regex_params = {} |
| 81 | + self.set_options(var_options=variables, direct=kwargs) |
| 82 | + server_url = self.get_option("server_url") |
| 83 | + site = self.get_option("site") |
| 84 | + api_auth_type = self.get_option("api_auth_type") or "bearer" |
| 85 | + api_auth_cookie = self.get_option("api_auth_cookie") |
| 86 | + automation_user = self.get_option("automation_user") |
| 87 | + automation_secret = self.get_option("automation_secret") |
| 88 | + validate_certs = self.get_option("validate_certs") |
| 89 | + |
| 90 | + site_url = server_url + "/" + site |
| 91 | + |
| 92 | + api = CheckMKLookupAPI( |
| 93 | + site_url=site_url, |
| 94 | + api_auth_type=api_auth_type, |
| 95 | + api_auth_cookie=api_auth_cookie, |
| 96 | + automation_user=automation_user, |
| 97 | + automation_secret=automation_secret, |
| 98 | + validate_certs=validate_certs, |
| 99 | + ) |
| 100 | + |
| 101 | + ret = [] |
| 102 | + |
| 103 | + for term in terms: |
| 104 | + # the activation_run API will return a status 404 when the job is no longer active so we better |
| 105 | + # use the background_job API |
| 106 | + response = json.loads(api.get("/objects/activation_run/%s" % term)) |
| 107 | + |
| 108 | + if "code" in response: |
| 109 | + raise AnsibleError( |
| 110 | + "Received error for %s - %s: %s" |
| 111 | + % ( |
| 112 | + response.get("url", ""), |
| 113 | + response.get("code", ""), |
| 114 | + response.get("msg", ""), |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + ret.append( |
| 119 | + "running" |
| 120 | + if response.get("extensions", {}).get("is_running") |
| 121 | + else "finished" |
| 122 | + ) |
| 123 | + |
| 124 | + return ret |
0 commit comments