-
Notifications
You must be signed in to change notification settings - Fork 4
/
nodeping_maintenance.py
233 lines (191 loc) · 6.74 KB
/
nodeping_maintenance.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, NodePing LLC <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
import traceback
from ansible.module_utils.basic import AnsibleModule
ANSIBLE_METADATA = {
"metadata_version": "3.0",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = """
---
module: nodeping_maintenance
short_description: Create scheduled or ad-hoc maintence schedules
version_added: "2.9"
description:
- This module allows you to create maintenance schedules that are
ad-hoc and only run once, or will operate at a recurring schedule.
More info on maintenance can be found here:
https://nodeping.com/docs-api-maintenance.html
requirements:
- "The NodePing Python library: https://github.com/NodePing/nodepingpy"
options:
token:
description:
- Your API token for your NodePing account
type: str
customerid:
description:
- The ID for your subaccount
type: str
checklist:
description:
- The list of checks that will be disabled when the maintenance schedule begins
type: list
name:
description:
- The name of the maintenance schedule
type: str
enabled:
description:
- Whether or not the maintenance will be enabled (True for ad-hoc)
type: bool
duration:
description:
- How long the maintenance will last once it is initiated
type: int
cron:
description:
- Cron expression for when a scheduled maintenance will start
type: str
scheduled:
description:
- Whether the maintenance should be scheduled (True) or ad-hoc (False)
type: bool
author:
- NodePing (@nodeping)
"""
EXAMPLES = """
---
- hosts: localhost
vars:
nodeping_api_token: your-token-here
tasks:
- name: Create ad-hoc maintenance
delegate_to: localhost
nodeping_maintenance:
token: "{{ nodeping_api_token }}"
name: ad-hoc maintenance
duration: 30
scheduled: False
checklist:
- 201911191441YC6SJ-4S9OJ78G
- 201911191441YC6SJ-XB5HUTG6
- name: Create maintenance schedule
delegate_to: localhost
nodeping_maintenance:
token: "{{ nodeping_api_token }}"
name: scheduled maintenance
scheduled: True
duration: 60
cron: 5 0 * 8 *
checklist:
- 201911191441YC6SJ-4S9OJ78G
- 201911191441YC6SJ-XB5HUTG6
"""
RETURN = """
original_message:
description: The original name param that was passed in
type: str
returned: always
message:
description: The response that NodePing returns after a maintenance is created
type: dict
returned: always
"""
NODEPING_IMPORT_ERROR = None
try:
from nodepingpy import maintenance
from nodepingpy.nptypes import maintenancetypes
except ImportError:
NODEPING_IMPORT_ERROR = traceback.format_exc()
IMPORTED_NODEPING_API = False
else:
IMPORTED_NODEPING_API = True
def configure_maintenance(parameters):
"""Create a scheduled or ad-hoc maintenance."""
if parameters["scheduled"]:
args_dict = generate_data(
parameters, maintenancetypes.ScheduledCreate.__annotations__.keys()
)
data = maintenancetypes.ScheduledCreate(**args_dict)
else:
args_dict = generate_data(
parameters, maintenancetypes.AdHocCreate.__annotations__.keys()
)
data = maintenancetypes.AdHocCreate(**args_dict)
result = maintenance.create(parameters["token"], data, parameters["customerid"])
if "error" in result.keys():
result.update({"changed": False})
return (False, parameters["name"], result)
else:
result.update({"changed": True})
return (True, parameters["name"], result)
def generate_data(parameters, matchkeys):
return_dict = {}
for key in parameters.keys():
if key in matchkeys:
return_dict.update({key: parameters[key]})
return return_dict
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
token=dict(type="str", required=True, no_log=True),
customerid=dict(type="str", required=False),
checklist=dict(type="list", required=True),
name=dict(type="str", required=True),
enabled=dict(type="bool", required=False, default=True),
duration=dict(type="int", required=True),
cron=dict(type="str", required=False),
scheduled=dict(type="bool", required=True),
)
# seed the result dict in the object
# we primarily care about changed and state
# change is if this module effectively modified the target
# state will include any data that you want your module to pass back
# for consumption, for example, in a subsequent task
result = dict(changed=False, original_message="", message="")
# the AnsibleModule object will be our abstraction working with Ansible
# this includes instantiation, a couple of common attr would be the
# args/params passed to the execution, as well as if the module
# supports check mode
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
if not IMPORTED_NODEPING_API:
module.fail_json(
msg="Missing import lib: nodepingpy", exception=NODEPING_IMPORT_ERROR
)
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
module.exit_json(**result)
params = module.params
status, label, output = configure_maintenance(params)
if not status:
module.fail_json(msg="Failed to create status %s" % label, **output)
# manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do)
result["original_message"] = ""
result["message"] = output
# use whatever logic you need to determine whether or not this module
# made any modifications to your target
try:
output["changed"]
except KeyError:
result["changed"] = False
else:
result["changed"] = output.pop("changed")
# during the execution of the module, if there is an exception or a
# conditional state that effectively causes a failure, run
# AnsibleModule.fail_json() to pass in the message and the result
# if result['changed'] == False:
# module.fail_json(msg='You requested this to fail', **result)
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
def main():
run_module()
if __name__ == "__main__":
main()