-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathicinga2_object.py
218 lines (181 loc) · 8.26 KB
/
icinga2_object.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
# pylint: disable=consider-using-f-string,super-with-arguments
import re
from ansible.plugins.action import ActionBase
from ansible.utils.vars import merge_hash
from ansible_collections.icinga.icinga.plugins.module_utils.parse import Icinga2Parser
class ActionModule(ActionBase):
def __init__(self, *args, **kwargs):
super(ActionModule, self).__init__(*args, **kwargs)
self.combined_constants_keys = None
self.icinga2_reserved = None
self.ensured_directories = set()
def run(self, tmp=None, task_vars=None):
result = super(ActionModule, self).run(tmp, task_vars)
# Check if 'objects' parameter is provided
if 'objects' in self._task.args:
# Process multiple objects
objects = self._task.args.get('objects', [])
if not objects:
return result # No objects to process
# Initialize overall result
aggregate_result = {
'changed': False,
'results': [],
'dest': [],
'failed': False,
}
# Cache constants and reserved words as instance variables
self.combined_constants_keys = list(task_vars['icinga2_combined_constants'].keys())
self.icinga2_reserved = task_vars['icinga2_reserved']
# Keep track of directories we've already ensured
self.ensured_directories = set()
# Process each object
for item in objects:
obj_result = self.process_object(item, tmp, task_vars)
if obj_result.get('failed'):
aggregate_result['failed'] = True
aggregate_result['msg'] = obj_result['msg']
return aggregate_result
aggregate_result['results'].append(obj_result)
aggregate_result['dest'].append(obj_result['dest'])
if obj_result.get('changed'):
aggregate_result['changed'] = True
# If only one object, flatten the result for backward compatibility
if len(aggregate_result['results']) == 1:
single_result = aggregate_result['results'][0]
aggregate_result.update(single_result)
# Remove 'results' and 'dest' list to match original structure
aggregate_result.pop('results', None)
aggregate_result['dest'] = single_result['dest']
else:
# Keep 'dest' as a list if multiple objects
pass
return aggregate_result
# Process a single object using individual arguments
args = self._task.args.copy()
# Cache constants and reserved words as instance variables
self.combined_constants_keys = list(task_vars['icinga2_combined_constants'].keys())
self.icinga2_reserved = task_vars['icinga2_reserved']
self.ensured_directories = set()
result = self.process_object(args, tmp, task_vars)
return result
def process_object(self, args, tmp, task_vars):
# Merge args
args = args.copy()
object_args = args.pop('args', {})
args = merge_hash(object_args, args)
object_type = args.pop('type', None)
if object_type not in task_vars['icinga2_object_types']:
return {'failed': True, 'msg': 'Unknown Icinga object type: %s' % object_type}
# Execute the module for the object type
obj = self._execute_module(
module_name='icinga2_' + object_type.lower(),
module_args=args,
task_vars=task_vars,
tmp=tmp
)
if obj.get('failed'):
return {'failed': True, 'msg': 'Call to module failed: %s' % obj['msg']}
if obj.get('skipped'):
return {'failed': True, 'msg': 'Call to module was skipped: %s' % obj['msg']}
# File path handling for assemble
path = task_vars['icinga2_fragments_path'] + '/' + obj['file'] + '/'
file_fragment = path + obj['order'] + '_' + object_type.lower() + '-' + obj['name']
# Initialize the result for this object
obj_result = {
'changed': False,
'dest': file_fragment,
'state': obj['state'],
'name': obj['name'],
'type': object_type,
}
if obj['state'] != 'absent':
varlist = [] # List of variables from 'apply for'
# Quoting of object name?
if obj['name'] not in task_vars['icinga2_combined_constants']:
object_name = '"' + obj['name'] + '"'
else:
object_name = obj['name']
# Apply rule?
if obj.get('apply'):
if not obj['args'].get('assign'):
return {'failed': True, 'msg': 'Apply rule %s is missing the assign rule.' % obj['name']}
object_content = 'apply ' + object_type
if obj.get('apply_target'):
object_content += ' ' + object_name + ' to ' + obj['apply_target']
elif obj.get('apply_for'):
object_content += ' for (' + obj['apply_for'] + ') '
r = re.search(r'^(.+)\s+in\s+', obj['apply_for'])
if r:
tmp_var = r.group(1).strip()
r2 = re.search(r'^(.+)=>(.+)$', tmp_var)
if r2:
varlist.extend([r2.group(1).strip(), r2.group(2).strip()])
else:
varlist.append(tmp_var)
else:
object_content += ' ' + object_name
# Template?
elif obj.get('template'):
object_content = 'template ' + object_type + ' ' + object_name
# Object
else:
object_content = 'object ' + object_type + ' ' + object_name
object_content += ' {\n'
# Imports?
if obj.get('imports'):
for item_import in obj['imports']:
if item_import.startswith('host.vars'):
object_content += ' import ' + str(item_import) + '\n'
else:
object_content += ' import "' + str(item_import) + '"\n'
object_content += '\n'
# Prepare keys for parsing
all_keys = self.combined_constants_keys + self.icinga2_reserved + varlist + list(obj['args'].keys())
parsed_content = Icinga2Parser().parse(obj['args'], all_keys, 2)
object_content += parsed_content + '}\n'
# Ensure directory exists (optimize by checking if already ensured)
if path not in self.ensured_directories:
file_args = {
'state': 'directory',
'path': path
}
file_module = self._execute_module(
module_name='file',
module_args=file_args,
task_vars=task_vars,
tmp=tmp
)
if file_module.get('changed', False):
obj_result['changed'] = True
# Add to ensured directories
self.ensured_directories.add(path)
# Write the object content to the file
copy_args = {
'dest': file_fragment,
'content': object_content
}
copy_module = self._execute_module(
module_name='copy',
module_args=copy_args,
task_vars=task_vars,
tmp=tmp
)
if copy_module.get('changed', False):
obj_result['changed'] = True
else:
# Remove file if it does not belong to a feature
if 'features-available' not in path:
file_args = {
'state': 'absent',
'path': file_fragment
}
file_module = self._execute_module(
module_name='file',
module_args=file_args,
task_vars=task_vars,
tmp=tmp
)
if file_module.get('changed', False):
obj_result['changed'] = True
return obj_result