Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/labels-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ module:timeperiod:
module:user:
- 'Component Name: user'

lookup:activation:
- 'Component Name: lookup_activation'

lookup:activations:
- 'Component Name: lookup_activations'

lookup:bakery:
- 'Component Name: lookup_bakery'

Expand Down
10 changes: 10 additions & 0 deletions .github/labels-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ module:user:
- changed-files:
- any-glob-to-any-file: 'plugins/modules/user.py'

lookup:activation:
- any:
- changed-files:
- any-glob-to-any-file: 'plugins/modules/lookup/activation.py'

lookup:activations:
- any:
- changed-files:
- any-glob-to-any-file: 'plugins/modules/lookup/activations.py'

lookup:bakery:
- any:
- changed-files:
Expand Down
120 changes: 120 additions & 0 deletions .github/workflows/ans-int-test-lkp-activation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# README:
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
#
# Resources:
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html

env:
NAMESPACE: checkmk
COLLECTION_NAME: general
MODULE_NAME: lookup_activation

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

name: Ansible Integration Tests for Activation Lookup Module
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
pull_request:
branches:
- main
- devel
paths:
- 'plugins/lookup/activation.py'
push:
paths:
- '.github/workflows/ans-int-test-lkp-activation.yaml'
- 'plugins/lookup/activation.py'
- 'plugins/module_utils/**'
- 'tests/integration/files/includes/**'
- 'tests/integration/targets/lookup_activation/**'

jobs:

integration:
runs-on: ubuntu-24.04
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
strategy:
fail-fast: false
matrix:
ansible:
- stable-2.18
- stable-2.19
- stable-2.20
- devel
python:
- '3.11'
- '3.12'
- '3.13'
exclude:
# Exclude unsupported sets.
- ansible: devel
python: '3.11'
- ansible: stable-2.20
python: '3.11'

services:
ancient_cre:
image: checkmk/check-mk-raw:2.2.0p47
ports:
- 5022:5000
env:
CMK_SITE_ID: "ancient_cre"
CMK_PASSWORD: "Sup3rSec4et!"
old_cre:
image: checkmk/check-mk-raw:2.3.0p41
ports:
- 5023:5000
env:
CMK_SITE_ID: "old_cre"
CMK_PASSWORD: "Sup3rSec4et!"
old_cme:
image: checkmk/check-mk-managed:2.3.0p41
ports:
- 5323:5000
env:
CMK_SITE_ID: "old_cme"
CMK_PASSWORD: "Sup3rSec4et!"
stable_cre:
image: checkmk/check-mk-raw:2.4.0p18
ports:
- 5024:5000
env:
CMK_SITE_ID: "stable_cre"
CMK_PASSWORD: "Sup3rSec4et!"
stable_cme:
image: checkmk/check-mk-managed:2.4.0p18
ports:
- 5324:5000
env:
CMK_SITE_ID: "stable_cme"
CMK_PASSWORD: "Sup3rSec4et!"

steps:
- name: Check out code
uses: actions/checkout@v6
with:
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: "Install uv and set the python version."
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python }}
enable-cache: true
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/

- name: "Setup uv venv."
run: uv venv
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Install ansible-base (${{ matrix.ansible }})
run: uv pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Run integration test
run: uv run ansible-test integration ${{env.MODULE_NAME}} -v --color --continue-on-error --diff --python ${{ matrix.python }}
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
120 changes: 120 additions & 0 deletions .github/workflows/ans-int-test-lkp-activations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# README:
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
#
# Resources:
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html

env:
NAMESPACE: checkmk
COLLECTION_NAME: general
MODULE_NAME: lookup_activations

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

name: Ansible Integration Tests for Activations Lookup Module
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
pull_request:
branches:
- main
- devel
paths:
- 'plugins/lookup/activations.py'
push:
paths:
- '.github/workflows/ans-int-test-lkp-activations.yaml'
- 'plugins/lookup/activations.py'
- 'plugins/module_utils/**'
- 'tests/integration/files/includes/**'
- 'tests/integration/targets/lookup_activations/**'

jobs:

integration:
runs-on: ubuntu-24.04
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
strategy:
fail-fast: false
matrix:
ansible:
- stable-2.18
- stable-2.19
- stable-2.20
- devel
python:
- '3.11'
- '3.12'
- '3.13'
exclude:
# Exclude unsupported sets.
- ansible: devel
python: '3.11'
- ansible: stable-2.20
python: '3.11'

services:
ancient_cre:
image: checkmk/check-mk-raw:2.2.0p47
ports:
- 5022:5000
env:
CMK_SITE_ID: "ancient_cre"
CMK_PASSWORD: "Sup3rSec4et!"
old_cre:
image: checkmk/check-mk-raw:2.3.0p41
ports:
- 5023:5000
env:
CMK_SITE_ID: "old_cre"
CMK_PASSWORD: "Sup3rSec4et!"
old_cme:
image: checkmk/check-mk-managed:2.3.0p41
ports:
- 5323:5000
env:
CMK_SITE_ID: "old_cme"
CMK_PASSWORD: "Sup3rSec4et!"
stable_cre:
image: checkmk/check-mk-raw:2.4.0p18
ports:
- 5024:5000
env:
CMK_SITE_ID: "stable_cre"
CMK_PASSWORD: "Sup3rSec4et!"
stable_cme:
image: checkmk/check-mk-managed:2.4.0p18
ports:
- 5324:5000
env:
CMK_SITE_ID: "stable_cme"
CMK_PASSWORD: "Sup3rSec4et!"

steps:
- name: Check out code
uses: actions/checkout@v6
with:
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: "Install uv and set the python version."
uses: astral-sh/setup-uv@v7
with:
python-version: ${{ matrix.python }}
enable-cache: true
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/

- name: "Setup uv venv."
run: uv venv
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Install ansible-base (${{ matrix.ansible }})
run: uv pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Run integration test
run: uv run ansible-test integration ${{env.MODULE_NAME}} -v --color --continue-on-error --diff --python ${{ matrix.python }}
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
124 changes: 124 additions & 0 deletions plugins/lookup/activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Copyright: (c) 2025, Robin Gierse <robin.gierse@checkmk.com>
# 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: activation
author: Robin Gierse (@robin-checkmk)
version_added: "6.8.0"

short_description: Get the status of a single activation

description:
- Returns the status of a single activation

options:

_terms:
description: activation ID to look up
required: True

extends_documentation_fragment: [checkmk.general.common_lookup]

notes:
- Like all lookups, this runs on the Ansible controller and is unaffected by other keywords such as 'become'.
If you need to use different permissions, you must change the command or run Ansible as another user.
- Alternatively, you can use a shell/command task that runs against localhost and registers the result.
- The directory of the play is used as the current working directory.
- It is B(NOT) possible to assign other variables to the variables mentioned in the C(vars) section!
This is a limitation of Ansible itself.
"""

EXAMPLES = """
- name: "Show activation status"
ansible.builtin.debug:
msg: "Activation status is {{ activation }}"
vars:
activation: "{{ lookup('checkmk.general.activation',
my_activation_id
server_url=http://myserver,
site=mysite,
validate_certs=False,
automation_user=automation_user,
automation_secret=automation_secret
)}}"

- name: "Use variables from inventory."
ansible.builtin.debug:
msg: "Activation status is {{ activation }}"
vars:
checkmk_var_server_url: "http://myserver/"
checkmk_var_site: "mysite"
checkmk_var_automation_user: "myuser"
checkmk_var_automation_secret: "mysecret"
checkmk_var_validate_certs: false
activation: "{{ lookup('checkmk.general.activation', my_activation_id) }}"
"""

RETURN = """
_list:
description:
- activation status
type: list
elements: str
"""

import json

from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible_collections.checkmk.general.plugins.module_utils.lookup_api import (
CheckMKLookupAPI,
)


class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
regex_params = {}
self.set_options(var_options=variables, direct=kwargs)
server_url = self.get_option("server_url")
site = self.get_option("site")
api_auth_type = self.get_option("api_auth_type") or "bearer"
api_auth_cookie = self.get_option("api_auth_cookie")
automation_user = self.get_option("automation_user")
automation_secret = self.get_option("automation_secret")
validate_certs = self.get_option("validate_certs")

site_url = server_url + "/" + site

api = CheckMKLookupAPI(
site_url=site_url,
api_auth_type=api_auth_type,
api_auth_cookie=api_auth_cookie,
automation_user=automation_user,
automation_secret=automation_secret,
validate_certs=validate_certs,
)

ret = []

for term in terms:
# the activation_run API will return a status 404 when the job is no longer active so we better
# use the background_job API
response = json.loads(api.get("/objects/activation_run/%s" % term))

if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)

ret.append(
"running"
if response.get("extensions", {}).get("is_running")
else "finished"
)

return ret
Loading