Skip to content

Commit 1e36f32

Browse files
committed
Add lookup modules for activations.
1 parent c68e710 commit 1e36f32

File tree

12 files changed

+821
-0
lines changed

12 files changed

+821
-0
lines changed

.github/labels-issues.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ module:timeperiod:
6565
module:user:
6666
- 'Component Name: user'
6767

68+
lookup:activation:
69+
- 'Component Name: lookup_activation'
70+
71+
lookup:activations:
72+
- 'Component Name: lookup_activations'
73+
6874
lookup:bakery:
6975
- 'Component Name: lookup_bakery'
7076

.github/labels-prs.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ module:user:
104104
- changed-files:
105105
- any-glob-to-any-file: 'plugins/modules/user.py'
106106

107+
lookup:activation:
108+
- any:
109+
- changed-files:
110+
- any-glob-to-any-file: 'plugins/modules/lookup/activation.py'
111+
112+
lookup:activations:
113+
- any:
114+
- changed-files:
115+
- any-glob-to-any-file: 'plugins/modules/lookup/activations.py'
116+
107117
lookup:bakery:
108118
- any:
109119
- changed-files:
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# README:
2+
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
3+
#
4+
# Resources:
5+
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
6+
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html
7+
8+
env:
9+
NAMESPACE: checkmk
10+
COLLECTION_NAME: general
11+
MODULE_NAME: lookup_activation
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
name: Ansible Integration Tests for Activation Lookup Module
18+
on:
19+
workflow_dispatch:
20+
schedule:
21+
- cron: '0 0 * * 0'
22+
pull_request:
23+
branches:
24+
- main
25+
- devel
26+
paths:
27+
- 'plugins/lookup/activation.py'
28+
push:
29+
paths:
30+
- '.github/workflows/ans-int-test-lkp-activation.yaml'
31+
- 'plugins/lookup/activation.py'
32+
- 'plugins/module_utils/**'
33+
- 'tests/integration/files/includes/**'
34+
- 'tests/integration/targets/lookup_activation/**'
35+
36+
jobs:
37+
38+
integration:
39+
runs-on: ubuntu-24.04
40+
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
ansible:
45+
- stable-2.18
46+
- stable-2.19
47+
- stable-2.20
48+
- devel
49+
python:
50+
- '3.11'
51+
- '3.12'
52+
- '3.13'
53+
exclude:
54+
# Exclude unsupported sets.
55+
- ansible: devel
56+
python: '3.11'
57+
- ansible: stable-2.20
58+
python: '3.11'
59+
60+
services:
61+
ancient_cre:
62+
image: checkmk/check-mk-raw:2.2.0p47
63+
ports:
64+
- 5022:5000
65+
env:
66+
CMK_SITE_ID: "ancient_cre"
67+
CMK_PASSWORD: "Sup3rSec4et!"
68+
old_cre:
69+
image: checkmk/check-mk-raw:2.3.0p41
70+
ports:
71+
- 5023:5000
72+
env:
73+
CMK_SITE_ID: "old_cre"
74+
CMK_PASSWORD: "Sup3rSec4et!"
75+
old_cme:
76+
image: checkmk/check-mk-managed:2.3.0p41
77+
ports:
78+
- 5323:5000
79+
env:
80+
CMK_SITE_ID: "old_cme"
81+
CMK_PASSWORD: "Sup3rSec4et!"
82+
stable_cre:
83+
image: checkmk/check-mk-raw:2.4.0p18
84+
ports:
85+
- 5024:5000
86+
env:
87+
CMK_SITE_ID: "stable_cre"
88+
CMK_PASSWORD: "Sup3rSec4et!"
89+
stable_cme:
90+
image: checkmk/check-mk-managed:2.4.0p18
91+
ports:
92+
- 5324:5000
93+
env:
94+
CMK_SITE_ID: "stable_cme"
95+
CMK_PASSWORD: "Sup3rSec4et!"
96+
97+
steps:
98+
- name: Check out code
99+
uses: actions/checkout@v6
100+
with:
101+
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
102+
103+
- name: "Install uv and set the python version."
104+
uses: astral-sh/setup-uv@v7
105+
with:
106+
python-version: ${{ matrix.python }}
107+
enable-cache: true
108+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/
109+
110+
- name: "Setup uv venv."
111+
run: uv venv
112+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
113+
114+
- name: Install ansible-base (${{ matrix.ansible }})
115+
run: uv pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz
116+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
117+
118+
- name: Run integration test
119+
run: uv run ansible-test integration ${{env.MODULE_NAME}} -v --color --continue-on-error --diff --python ${{ matrix.python }}
120+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# README:
2+
# - When changing the module name, it needs to be changed in 'env:MODULE_NAME' and in 'on:pull_requests:path'!
3+
#
4+
# Resources:
5+
# - Template for this file: https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
6+
# - About Ansible integration tests: https://docs.ansible.com/ansible/latest/dev_guide/testing_integration.html
7+
8+
env:
9+
NAMESPACE: checkmk
10+
COLLECTION_NAME: general
11+
MODULE_NAME: lookup_activations
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
name: Ansible Integration Tests for Activations Lookup Module
18+
on:
19+
workflow_dispatch:
20+
schedule:
21+
- cron: '0 0 * * 0'
22+
pull_request:
23+
branches:
24+
- main
25+
- devel
26+
paths:
27+
- 'plugins/lookup/activations.py'
28+
push:
29+
paths:
30+
- '.github/workflows/ans-int-test-lkp-activations.yaml'
31+
- 'plugins/lookup/activations.py'
32+
- 'plugins/module_utils/**'
33+
- 'tests/integration/files/includes/**'
34+
- 'tests/integration/targets/lookup_activations/**'
35+
36+
jobs:
37+
38+
integration:
39+
runs-on: ubuntu-24.04
40+
name: Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
ansible:
45+
- stable-2.18
46+
- stable-2.19
47+
- stable-2.20
48+
- devel
49+
python:
50+
- '3.11'
51+
- '3.12'
52+
- '3.13'
53+
exclude:
54+
# Exclude unsupported sets.
55+
- ansible: devel
56+
python: '3.11'
57+
- ansible: stable-2.20
58+
python: '3.11'
59+
60+
services:
61+
ancient_cre:
62+
image: checkmk/check-mk-raw:2.2.0p47
63+
ports:
64+
- 5022:5000
65+
env:
66+
CMK_SITE_ID: "ancient_cre"
67+
CMK_PASSWORD: "Sup3rSec4et!"
68+
old_cre:
69+
image: checkmk/check-mk-raw:2.3.0p41
70+
ports:
71+
- 5023:5000
72+
env:
73+
CMK_SITE_ID: "old_cre"
74+
CMK_PASSWORD: "Sup3rSec4et!"
75+
old_cme:
76+
image: checkmk/check-mk-managed:2.3.0p41
77+
ports:
78+
- 5323:5000
79+
env:
80+
CMK_SITE_ID: "old_cme"
81+
CMK_PASSWORD: "Sup3rSec4et!"
82+
stable_cre:
83+
image: checkmk/check-mk-raw:2.4.0p18
84+
ports:
85+
- 5024:5000
86+
env:
87+
CMK_SITE_ID: "stable_cre"
88+
CMK_PASSWORD: "Sup3rSec4et!"
89+
stable_cme:
90+
image: checkmk/check-mk-managed:2.4.0p18
91+
ports:
92+
- 5324:5000
93+
env:
94+
CMK_SITE_ID: "stable_cme"
95+
CMK_PASSWORD: "Sup3rSec4et!"
96+
97+
steps:
98+
- name: Check out code
99+
uses: actions/checkout@v6
100+
with:
101+
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
102+
103+
- name: "Install uv and set the python version."
104+
uses: astral-sh/setup-uv@v7
105+
with:
106+
python-version: ${{ matrix.python }}
107+
enable-cache: true
108+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}/
109+
110+
- name: "Setup uv venv."
111+
run: uv venv
112+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
113+
114+
- name: Install ansible-base (${{ matrix.ansible }})
115+
run: uv pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible }}.tar.gz
116+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
117+
118+
- name: Run integration test
119+
run: uv run ansible-test integration ${{env.MODULE_NAME}} -v --color --continue-on-error --diff --python ${{ matrix.python }}
120+
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

plugins/lookup/activation.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)