-
Notifications
You must be signed in to change notification settings - Fork 57
/
bakery.py
161 lines (137 loc) · 4.87 KB
/
bakery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright: (c) 2023, Max Sickora <[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: bakery
author: Max Sickora (@max-checkmk)
version_added: "4.0.0"
short_description: Get the bakery status of a Checkmk server
description:
- Returns the bakery status of a Checkmk server as a string, e.g. 'running'
options:
server_url:
description: URL of the Checkmk server
required: True
vars:
- name: ansible_lookup_checkmk_server_url
env:
- name: ANSIBLE_LOOKUP_CHECKMK_SERVER_URL
ini:
- section: checkmk_lookup
key: server_url
site:
description: Site name.
required: True
vars:
- name: ansible_lookup_checkmk_site
env:
- name: ANSIBLE_LOOKUP_CHECKMK_SITE
ini:
- section: checkmk_lookup
key: site
automation_user:
description: Automation user for the REST API access.
required: True
vars:
- name: ansible_lookup_checkmk_automation_user
env:
- name: ANSIBLE_LOOKUP_CHECKMK_AUTOMATION_USER
ini:
- section: checkmk_lookup
key: automation_user
automation_secret:
description: Automation secret for the REST API access.
required: True
vars:
- name: ansible_lookup_checkmk_automation_secret
env:
- name: ANSIBLE_LOOKUP_CHECKMK_AUTOMATION_SECRET
ini:
- section: checkmk_lookup
key: automation_secret
validate_certs:
description: Whether or not to validate TLS certificates.
type: boolean
required: False
default: True
vars:
- name: ansible_lookup_checkmk_validate_certs
env:
- name: ANSIBLE_LOOKUP_CHECKMK_VALIDATE_CERTS
ini:
- section: checkmk_lookup
key: validate_certs
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 bakery status"
ansible.builtin.debug:
msg: "Bakery status is {{ bakery }}"
vars:
bakery: "{{ lookup('checkmk.general.bakery',
server_url=http://myserver,
site=mysite,
validate_certs=False,
automation_user=automation_user,
automation_secret=automation_secret
)}}"
- name: "Use variables outside the module call."
ansible.builtin.debug:
msg: "Bakery status is {{ bakery }}"
vars:
ansible_lookup_checkmk_server_url: "http://myserver/"
ansible_lookup_checkmk_site: "mysite"
ansible_lookup_checkmk_automation_user: "myuser"
ansible_lookup_checkmk_automation_secret: "mysecret"
ansible_lookup_checkmk_validate_certs: false
bakery: "{{ lookup('checkmk.general.bakery') }}"
"""
RETURN = """
_list:
description:
- server bakery 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):
self.set_options(var_options=variables, direct=kwargs)
server_url = self.get_option("server_url")
site = self.get_option("site")
user = self.get_option("automation_user")
secret = self.get_option("automation_secret")
validate_certs = self.get_option("validate_certs")
ret = []
api = CheckMKLookupAPI(
site_url=server_url + "/" + site,
user=user,
secret=secret,
validate_certs=validate_certs,
)
response = json.loads(
api.get("/domain-types/agent/actions/baking_status/invoke")
)
if "code" in response:
raise AnsibleError(
"Received error for %s - %s: %s"
% (
response.get("url", ""),
response.get("code", ""),
response.get("msg", ""),
)
)
ret.append(response.get("result", {}).get("value").get("state"))
return ret