forked from Nosmoht/ansible-module-foreman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforeman_host.py
408 lines (364 loc) · 15 KB
/
foreman_host.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: foreman_host
short_description: Create, update and delete hosts with Foreman using Foreman API v2
description:
- Create and delete hosts using Foreman API v2
options:
name:
description: Hostgroup name
required: false
default: None
architecture:
description: Architecture name
required: false
default: x86_64
build:
description: Boolean to define if host should be builded
required: false
default: false
compute_profile:
description: Compute Profile name
required: false
default: None
compute_resource:
description: Compute Resource name
required: false
default: None
domain:
description: Domain name
required: false
default: None
enabled:
description: Host enabled
required: false
choices: BOOLEANS
environment:
description: Name of environment used by default
required: false
default: None
hostgroup:
description: Hostgroup name
required: false
default: None
image:
description: Image name to be used if creating from image
required: false
default: None
location:
description: Location name (Only useful with Katello)
required: false
default: None
managed:
description: Should Foreman manage the host
required: false
default: false
medium:
description: Medium name
required: false
default: None
operatingsystem:
descrtiption: Operatingsystem name
required: false
default: None
organization:
description: Organization name (only useful with Katello)
required: false
default: None
parameters:
description: List of parameters and values
required: false
default: None
provision_method:
description: How to provision the host
required: false
default: None
choices: ['build', 'image']
root_pass:
description: root password
required: false
default: None
subnet:
description: Name of subnet to use for this host
required: false
default: null
foreman_host:
description: Hostname or IP address of Foreman system
required: false
default: 127.0.0.1
foreman_port:
description: Port of Foreman API
required: false
default: 443
foreman_user:
description: Username to be used to authenticate on Foreman
required: true
default: null
foreman_pass:
description: Password to be used to authenticate user on Foreman
required: true
default: null
notes:
- Requires the python-foreman package to be installed. See https://github.com/Nosmoht/python-foreman.
author: Thomas Krahn
'''
try:
from foreman.foreman import *
except ImportError:
foremanclient_found = False
else:
foremanclient_found = True
def get_resource(resource_type, resource_func, resource_name):
try:
result = resource_func(data=dict(name=resource_name))
if not result:
module.fail_json(msg='{resource_type} {resource_name} not found'.format(resource_type=resource_type,
resource_name=resource_name))
except ForemanError as e:
module.fail_json(
msg='Error while getting {resource_type}: {error}'.format(resource_type=resource_type, error=e.message))
return result
def ensure():
changed = False
name = module.params['name']
architecture_name = module.params[ARCHITECTURE]
build = module.params['build']
compute_profile_name = module.params[COMPUTE_PROFILE]
compute_resource_name = module.params[COMPUTE_RESOURCE]
domain_name = module.params[DOMAIN]
enabled = module.params['enabled']
environment_name = module.params[ENVIRONMENT]
hostgroup_name = module.params[HOSTGROUP]
image_name = module.params['image']
location_name = module.params[LOCATION]
managed = module.params['managed']
medium_name = module.params[MEDIUM]
operatingsystem_name = module.params[OPERATINGSYSTEM]
organization_name = module.params[ORGANIZATION]
parameters = module.params['parameters']
provision_method = module.params['provision_method']
root_pass = module.params['root_pass']
state = module.params['state']
subnet_name = module.params[SUBNET]
foreman_host = module.params['foreman_host']
foreman_port = module.params['foreman_port']
foreman_user = module.params['foreman_user']
foreman_pass = module.params['foreman_pass']
theforeman = Foreman(hostname=foreman_host,
port=foreman_port,
username=foreman_user,
password=foreman_pass)
if domain_name:
if domain_name in name:
host_name = name
else:
host_name = '{name}.{domain}'.format(name=name, domain=domain_name)
else:
host_name = name
data = dict(name=host_name)
try:
host = theforeman.search_host(data=data)
except ForemanError as e:
module.fail_json(msg='Error while searching host: {0}'.format(e.message))
if state == 'absent':
if host:
try:
host = theforeman.delete_host(id=host.get('id'))
return True, host
except ForemanError as e:
module.fail_json(msg='Could not delete host: {0}'.format(e.message))
return False, host
if not host:
# Architecture
if architecture_name:
architecture = get_resource(resource_type=ARCHITECTURE,
resource_func=theforeman.search_architecture,
resource_name=architecture_name)
data['architecture_id'] = architecture.get('id')
# Build
data['build'] = build
# Compute Profile
if compute_profile_name:
compute_profile = get_resource(resource_type=COMPUTE_PROFILE,
resource_func=theforeman.search_compute_profile,
resource_name=compute_profile_name)
data['compute_profile_id'] = compute_profile.get('id')
# Compute Resource
if compute_resource_name:
compute_resource = get_resource(resource_type=COMPUTE_RESOURCE,
resource_func=theforeman.search_compute_resource,
resource_name=compute_resource_name)
data['compute_resource_id'] = compute_resource.get('id')
# Image
if image_name:
compute_resource_images = compute_resource.get('images')
if not compute_resource_images:
module.fail_json(msg='Compute Resource {0} has no images'.format(compute_resource_name))
images = filter(lambda x: x['name'] == image_name, compute_resource_images)
if len(images) == 0:
module.fail_json(
msg='Could not find image {image_name} in compute resource {compute_resource}'.format(
image_name=image_name, compute_resource=compute_resource_name))
if len(images) > 1:
module.fail_json(
msg='Found {count} images named {image_name} in compute resource {compute_resource}'.format(
count=len(images), image_name=image_name, compute_resource=compute_resource_images))
image = images[0]
data['image_id'] = image.get('id')
# Domain
if domain_name:
domain = get_resource(resource_type=DOMAIN,
resource_func=theforeman.search_domain,
resource_name=domain_name)
data['domain_id'] = domain.get('id')
# Enabled
data['enabled'] = enabled
# Environment
if environment_name:
environment = get_resource(resource_type=ENVIRONMENT,
resource_func=theforeman.search_environment,
resource_name=environment_name)
data['environment_id'] = environment.get('id')
# Hostgroup
if hostgroup_name:
hostgroup = get_resource(resource_type=HOSTGROUP,
resource_func=theforeman.search_hostgroup,
resource_name=hostgroup_name)
data['hostgroup_id'] = hostgroup.get('id')
# Location
if location_name:
location = get_resource(resource_type=LOCATION,
resource_func=theforeman.search_location,
resource_name=location_name)
data['location_id'] = location.get('id')
# Managed
data['managed'] = managed
# Medium
if medium_name:
medium = get_resource(resource_type=MEDIUM,
resource_func=theforeman.search_medium,
resource_name=medium_name)
data['medium_id'] = medium.get('id')
# Organization
if organization_name:
organization = get_resource(resource_type=ORGANIZATION,
resource_func=theforeman.search_organization,
resource_name=organization_name)
data['organization_id'] = organization.get('id')
# Operatingssystem
if operatingsystem_name:
operatingsystem = get_resource(resource_type=OPERATINGSYSTEM,
resource_func=theforeman.search_operatingsystem,
resource_name=operatingsystem_name)
data['operatingsystem_id'] = operatingsystem.get('id')
# Provision Method
if provision_method:
data['provision_method'] = provision_method
# Root password
if root_pass:
data['root_pass'] = root_pass
# Subnet
if subnet_name:
subnet = get_resource(resource_type=SUBNET,
resource_func=theforeman.search_subnet,
resource_name=subnet_name)
data['subnet_id'] = subnet.get('id')
try:
host = theforeman.create_host(data=data)
except ForemanError as e:
module.fail_json(msg='Could not create host: {0}'.format(e.message))
changed = True
host_id = host.get('id')
# Parameters
if parameters:
try:
host_parameters = theforeman.get_host_parameters(host_id=host_id)
except ForemanError as e:
module.fail_json(msg='Could not get host parameters: {0}'.format(e.message))
for param in parameters:
host_params = [item for item in host_parameters if item.get('name') == param.get('name')]
if not host_params:
try:
theforeman.create_host_parameter(host_id=host_id, data=param)
except ForemanError as e:
module.fail_json(
msg='Could not create host parameter {param_name}: {error}'.format(param_name=param.get('name'),
error=e.message))
changed = True
else:
for host_param in host_params:
# Replace \n sems to be needed. Otherwise some strings are always changed evenso they look equal
if host_param.get('value').replace('\n', '') != param.get('value').replace('\n', ''):
try:
theforeman.update_host_parameter(host_id=host_id,
parameter_id=host_param.get('id'),
data=param)
except ForemanError as e:
module.fail_json(
msg='Could not update host parameter {param_name}: {error}'.format(
param_name=param.get('name'), error=e.message))
changed = True
try:
host_power = theforeman.get_host_power(host_id=host_id)
except ForemanError as e:
module.fail_json(msg='Could not get host power information: {0}'.format(e.message))
host_power_state = host_power.get('power')
if state == 'rebooted':
try:
theforeman.reboot_host(host_id=host_id)
changed = True
except ForemanError as e:
module.fail_json(msg='Could not reboot host: {0}'.format(e.message))
elif state == 'running' and host_power_state != 'poweredOn':
try:
theforeman.poweron_host(host_id=host_id)
changed = True
except ForemanError as e:
module.fail_json(msg='Could not power on host: {0}'.format(e.message))
elif state == 'stopped' and host_power_state != 'poweredOff':
try:
theforeman.poweroff_host(host_id=host_id)
changed = True
except ForemanError as e:
module.fail_json(msg='Could not power off host: {0}'.format(e.message))
return changed, host
def main():
global module
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
architecture=dict(type='str', default='x86_64'),
build=dict(type='bool', default=False),
compute_profile=dict(type='str', default=None),
compute_resource=dict(type='str', default=None),
domain=dict(type='str', default=None),
enabled=dict(type='bool', default=False),
environment=dict(type='str', default=None),
hostgroup=dict(type='str', default=None),
image=dict(type='str', default=None),
location=dict(type='str', default=None),
managed=dict(type='bool', default=False),
medium=dict(type='str', default=None),
operatingsystem=dict(type='str', default=None),
organization=dict(type='str', default=None),
parameters=dict(type='list', default=None),
provision_method=dict(type='str', required=False, choices=['build', 'image']),
root_pass=dict(type='str', default=None),
state=dict(type='str', default='present',
choices=['present', 'absent', 'running', 'stopped', 'rebooted']),
subnet=dict(type='str', default=None),
foreman_host=dict(type='str', default='127.0.0.1'),
foreman_port=dict(type='str', default='443'),
foreman_user=dict(type='str', required=True),
foreman_pass=dict(type='str', required=True)
),
)
if not foremanclient_found:
module.fail_json(msg='python-foreman module is required. See https://github.com/Nosmoht/python-foreman.')
changed, host = ensure()
module.exit_json(changed=changed, host=host)
# import module snippets
from ansible.module_utils.basic import *
main()