,

Streamlining IT Operations: Ansible Automation with CheckMK

MarkBurgessMark Burgess  |  

Introduction

In the dynamic landscape of IT operations, efficiency is paramount. Automation tools play a crucial role in achieving operational excellence by reducing manual efforts and ensuring consistency across diverse environments. Ansible, a popular open-source automation tool, is widely used for configuration management, application deployment, and task automation. In this blog post, we will explore how to enhance Ansible’s capabilities when used with the CheckMK monitoring platform.

Understanding Ansible and CheckMK

Ansible

Ansible is a powerful automation platform that simplifies complex tasks, allowing IT professionals to automate repetitive operations and manage infrastructure as code. Ansible uses YAML syntax for playbooks, making it easy to understand and write automation scripts.

CheckMK

CheckMK is a comprehensive monitoring solution that provides real-time insights into the health and performance of IT infrastructure. It excels at monitoring servers, networks, and applications, offering a centralised view of the entire environment.

Integrating Ansible with CheckMK

Automating Ansible with CheckMK offers several benefits:

  • CheckMK provides a detailed inventory of host and service information of monitored assets in the IT environment.
  • Ansible can consume a dynamic inventory source when running playbooks.
  • CheckMK provides powerful tagging and grouping capabilities for hosts and services in the IT environment.
  • CheckMK host groups can be mapped directly to Ansible inventory groups.
  • CheckMK scales well to thousands of monitored hosts and services.

Using CheckMK ensures your Ansible playbooks always operate with the latest and most accurate information about your infrastructure.

Getting Started

1. Configure Tags & Host Grouping

One very powerful feature of CheckMK is host and service grouping and labelling.

Hosts can be assigned to host groups explicitly by hostname, or dynamically using host tags. Host groups are able to be used in API queries or as a filter for custom views.

Labels can be assigned to hosts or services either explicitly or dynamically using rules.

Once defined, host groups can be used as the target hostname filter in the playbook and labels can be used in your Ansible playbooks as reference variables. This enhances the flexibility and precision of your automation tasks and significantly reduces the overhead of maintaining a static Ansible inventory.

The following example shows how CheckMK host groups can be passed to a playbook for task execution on the hosts in the specified group:

CheckMK Host Group: CMK_HOST_GROUP_1 (JSON Export)

[
 [
  "host_state",
  "host",
  "host_icons",
  "num_services_ok",
  "num_services_warn",
  "num_services_unknown",
  "num_services_crit",
  "num_services_pending"
 ],
 [
  "UP",
  "admin-cmk-01",
  [],
  "33",
  "0",
  "0",
  "0",
  "0"
 ],
 [
  "UP",
  "admin-cmk-02",
  [],
  "21",
  "2",
  "0",
  "1",
  "0"
 ],
 [
  "UP",
  "admin-cmk-03",
  [],
  "25",
  "0",
  "0",
  "0",
  "0"
 ]
]

When running the following playbook and passing CMK_HOST_GROUP_1 as the target variable, the playbook will execute on all the hosts in the CMK_HOST_GROUP_1 host group.

/usr/bin/ansible-playbook -i /usr/local/bin/get_pb_dyn_inventory.sh -e target=CMK_HOST_GROUP_1 print_facts.yml

- hosts: "{{ target }}"
  tasks:
    - debug:
        var: ansible_facts

The following example shows how CheckMK host labels can be used within playbooks as variables:

The dynamic inventory script will generate the correct JSON inventory format that assigns CheckMK host labels as Ansible hostvars variables.

/usr/bin/ansible-playbook -i /usr/local/bin/get_pb_dyn_inventory.sh -e target=CMK_HOST_GROUP_1 print_facts.yml


- hosts: "{{ target }}"
  tasks:
    - debug:
        var: ansible_facts
    when: ( hostvars[inventory_hostname].host_tier is search("db") ) and ( hostvars[inventory_hostname].environment is search("DEV1") )

Watch this video to learn more about host grouping.

2. Automation User Configuration

We use the built-in automation user for API access. Once configured, the API key for the automation user is used when calling CheckMK to generate the Ansible inventory.

3. CheckMK Inventory View

Create a CheckMK custom view that has the required host fields to generate the Ansible dynamic inventory. Two custom views are required by the inventory plugin:

Name: AllHostsByGroup
Fields: 
  - Host groups: Host group name
  - Hosts: Hostname
  - Hosts: Host labels

Name: AllHostLabels
Fields:
  - Hosts: Hostname
  - Hosts: Host labels

4. Ansible Dynamic Inventory Plugin

We built a Python script and bash wrapper that generates the Ansible inventory. The two scripts can be found here. Make sure to update the relevant parameters for your CheckMK instance and API credentials.

5. Ansible Playbook

We have a simple Ansible playbook here that uses the CheckMK inventory to generate a list of hosts. The sky is the limit on how you use the CheckMK inventory for Ansible once you have this in place.

Benefits of Automation with CheckMK Inventory

Dynamic Updates:

The CheckMK inventory plugin ensures that your Ansible inventory is always up-to-date, reflecting any changes in your infrastructure. This dynamic approach eliminates the need for manual updates, saving time and reducing the risk of errors.

Enhanced Visibility:

By integrating Ansible with CheckMK, you gain a holistic view of your infrastructure’s health and performance. This synergy enables IT teams to make informed decisions and respond proactively to potential issues.

Simplified Management:

Automation with CheckMK inventory streamlines the management of large and complex environments. Whether you’re scaling your infrastructure or implementing changes, the combination of Ansible and CheckMK provides a seamless and efficient solution.

Reduced Cost

Our IT service operations benefit from the combined use of CheckMK and Ansible, resulting in cost savings through both time efficiency and automated cloud resource management. Read how we use this technology to support Oracle Cloud Infrastructure.

Conclusion

In the ever-evolving landscape of IT operations, automation is not just a luxury but a necessity. By integrating Ansible with CheckMK inventory, you empower your organisation to achieve operational efficiency, reduce manual efforts, and ensure a proactive approach to infrastructure management. Embrace the power of automation and elevate your IT operations to new heights with the dynamic capabilities of Ansible and CheckMK.

Interested to learn more how you can use these tools to reduce cost and improve efficiency? Book a call with us today to discuss.

About the Author

Leave a comment

Send this to a friend