Skip to content

Commit

Permalink
Merge pull request #415 from Checkmk/feature/lookup-version
Browse files Browse the repository at this point in the history
Add a Lookup Plugin to get the Checkmk Version of a Server
  • Loading branch information
robin-checkmk committed Aug 18, 2023
2 parents 7233d35 + 91371b7 commit 86d7c4e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/lookup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
major_changes:
- Version lookup plugin - Add Version lookup plugin.
2 changes: 2 additions & 0 deletions playbooks/demo/full.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
- name: "Lookup."
ansible.builtin.import_playbook: lookup.yml
- name: "Hosts and Folders."
ansible.builtin.import_playbook: hosts-and-folders.yml
- name: "Groups."
Expand Down
22 changes: 22 additions & 0 deletions playbooks/demo/lookup.yml
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'
110 changes: 110 additions & 0 deletions plugins/lookup/version.py
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

0 comments on commit 86d7c4e

Please sign in to comment.