-
Notifications
You must be signed in to change notification settings - Fork 57
/
bakery.py
153 lines (125 loc) · 4.11 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
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# Copyright: (c) 2023, Max Sickora <max.sickora@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 = r"""
---
module: bakery
short_description: Trigger baking and signing in the agent bakery.
# If this is part of a collection, you need to use semantic versioning,
# i.e. the version is of the form "2.5.0" and not "2.4".
version_added: "0.21.0"
description:
- Trigger baking and signing in the agent bakery.
extends_documentation_fragment: [checkmk.general.common]
options:
signature_key_id:
description: The id of the signing key
required: false
type: int
signature_key_passphrase:
description: The passphrase of the signing key
required: false
type: str
state:
description: State - Baked, signed or baked and signed
required: true
choices: ["baked", "signed", "baked_signed"]
type: str
author:
- Max Sickora (@max-checkmk)
"""
EXAMPLES = r"""
# Bake all agents without signing, as example in a fresh installation without a signature key.
- name: "Bake all agents without signing."
checkmk.general.bakery:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
state: "baked"
# Sign all agents.
- name: "Sign all agents."
checkmk.general.bakery:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
signature_key_id: 1
signature_key_passphrase: "my_key"
state: "signed"
# Bake and sign all agents.
- name: "Bake and sign all agents."
checkmk.general.bakery:
server_url: "http://myserver/"
site: "mysite"
automation_user: "myuser"
automation_secret: "mysecret"
signature_key_id: 1
signature_key_passphrase: "my_key"
state: "baked_signed"
"""
RETURN = r"""
http_code:
description: The HTTP code the Checkmk API returns.
type: int
returned: always
sample: '200'
message:
description: The output message that the module generates.
type: str
returned: always
sample: 'Done.'
"""
import time
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.checkmk.general.plugins.module_utils.api import CheckmkAPI
from ansible_collections.checkmk.general.plugins.module_utils.utils import (
result_as_dict,
)
class BakeryAPI(CheckmkAPI):
def post(self):
if self.params.get("state", "") != "baked":
data = {
"key_id": self.params.get("signature_key_id", ""),
"passphrase": self.params.get("signature_key_passphrase", ""),
}
else:
data = ""
if self.params.get("state", "") == "baked":
action = "bake"
if self.params.get("state", "") == "signed":
action = "sign"
if self.params.get("state", "") == "baked_signed":
action = "bake_and_sign"
return self._fetch(
endpoint="/domain-types/agent/actions/%s/invoke" % action,
data=data,
method="POST",
)
def run_module():
module_args = dict(
server_url=dict(type="str", required=True),
site=dict(type="str", required=True),
validate_certs=dict(type="bool", required=False, default=True),
automation_user=dict(type="str", required=True),
automation_secret=dict(type="str", required=True, no_log=True),
signature_key_id=dict(type="int", required=False),
signature_key_passphrase=dict(type="str", required=False, no_log=True),
state=dict(
type="str",
choices=["baked", "signed", "baked_signed"],
required=True,
),
)
module = AnsibleModule(argument_spec=module_args, supports_check_mode=False)
bakery = BakeryAPI(module)
result = bakery.post()
time.sleep(3)
module.exit_json(**result_as_dict(result))
def main():
run_module()
if __name__ == "__main__":
main()