-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from Checkmk/feature/lookup-version
Add a Lookup Plugin to get the Checkmk Version of a Server
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
major_changes: | ||
- Version lookup plugin - Add Version lookup plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
- name: "Showcase Lookup Plugins." | ||
hosts: test | ||
strategy: linear | ||
gather_facts: false | ||
vars_files: | ||
- ../vars/auth.yml # This vars file provides details about your site | ||
tasks: | ||
|
||
- name: "Get Checkmk version." | ||
debug: | ||
msg: "Version is {{ version }}" | ||
vars: | ||
version: "{{ lookup('checkmk.general.version', | ||
server_url + '/' + site, | ||
validate_certs=False, | ||
automation_user=automation_user, | ||
automation_secret=automation_secret | ||
)}}" | ||
|
||
delegate_to: localhost | ||
run_once: 'true' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright: (c) 2023, Lars Getwan <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
DOCUMENTATION = """ | ||
name: version | ||
author: Lars Getwan (@lgetwan) | ||
version_added: "3.1.0" | ||
short_description: Get the version of a Checkmk server | ||
description: | ||
- Returns the version of a Checkmk server as a string, e.g. '2.1.0p31.cre' | ||
options: | ||
_terms: | ||
description: site url | ||
required: True | ||
automation_user: | ||
description: automation user for the REST API access | ||
required: True | ||
automation_secret: | ||
description: automation secret for the REST API access | ||
required: True | ||
validate_certs: | ||
description: Wether or not to validate TLS cerificates | ||
type: boolean | ||
required: False | ||
default: True | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: "Show Checkmk version" | ||
debug: | ||
msg: "Server version is {{ version }}" | ||
vars: | ||
version: "{{ lookup('checkmk.general.version', | ||
server_url + '/' + site, | ||
validate_certs=False, | ||
automation_user=automation_user, | ||
automation_secret=automation_secret | ||
)}}" | ||
""" | ||
|
||
RETURN = """ | ||
_list: | ||
description: | ||
- server Checkmk version | ||
type: list | ||
elements: str | ||
""" | ||
|
||
import json | ||
from urllib.error import HTTPError, URLError | ||
|
||
from ansible.errors import AnsibleError | ||
from ansible.module_utils.common.text.converters import to_native, to_text | ||
from ansible.module_utils.urls import ConnectionError, SSLValidationError, open_url | ||
from ansible.plugins.lookup import LookupBase | ||
|
||
|
||
class LookupModule(LookupBase): | ||
def run(self, terms, variables, **kwargs): | ||
|
||
self.set_options(var_options=variables, direct=kwargs) | ||
user = self.get_option("automation_user") | ||
secret = self.get_option("automation_secret") | ||
validate_certs = self.get_option("validate_certs") | ||
|
||
ret = [] | ||
for term in terms: | ||
base_url = term + "/check_mk/api/1.0" | ||
api_endpoint = "/version" | ||
url = base_url + api_endpoint | ||
|
||
headers = { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
"Authorization": "Bearer %s %s" % (user, secret), | ||
} | ||
|
||
try: | ||
response = open_url( | ||
url, | ||
data=None, | ||
headers=headers, | ||
method="GET", | ||
validate_certs=validate_certs, | ||
) | ||
|
||
except HTTPError as e: | ||
raise AnsibleError( | ||
"Received HTTP error for %s : %s" % (url, to_native(e)) | ||
) | ||
except URLError as e: | ||
raise AnsibleError( | ||
"Failed lookup url for %s : %s" % (url, to_native(e)) | ||
) | ||
except SSLValidationError as e: | ||
raise AnsibleError( | ||
"Error validating the server's certificate for %s: %s" | ||
% (url, to_native(e)) | ||
) | ||
except ConnectionError as e: | ||
raise AnsibleError("Error connecting to %s: %s" % (url, to_native(e))) | ||
|
||
checkmkinfo = json.loads(to_text(response.read())) | ||
ret.append(checkmkinfo.get("versions").get("checkmk")) | ||
|
||
return ret |