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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: keystone_service
short_description: Manage OpenStack Identity (keystone) service endpoints
options:
name:
description:
- name of service (e.g., keystone)
required: yes
type:
description:
- type of service (e.g., identity)
required: yes
description:
description:
- description of service (e.g., Identity Service)
required: yes
public_url:
description:
- public url of service.
- 'Alias: I(url)'
- 'Alias: I(publicurl)'
required: yes
internal_url:
description:
- internal url of service.
- 'Alias: I(internalurl)'
required: no
default: value of public_url
admin_url:
description:
- admin url of service.
- 'Alias: I(adminurl)'
required: no
default: value of public_url
insecure:
description:
- allow use of self-signed SSL certificates
required: no
choices: [ "yes", "no" ]
default: no
region:
description:
- region of service
required: no
default: RegionOne
ignore_other_regions:
description:
- allow endpoint to exist in other regions
required: no
choices: [ "yes", "no" ]
default: no
state:
description:
- Indicate desired state of the resource
choices: ['present', 'absent']
default: present
requirements: [ python-keystoneclient ]
author: Lorin Hochstein
'''
EXAMPLES = '''
examples:
keystone_service: >
name=keystone
type=identity
description="Keystone Identity Service"
publicurl=http://192.168.206.130:5000/v2.0
internalurl=http://192.168.206.130:5000/v2.0
adminurl=http://192.168.206.130:35357/v2.0
keystone_service: >
name=glance
type=image
description="Glance Identity Service"
url=http://192.168.206.130:9292
'''
try:
from keystoneclient.v2_0 import client
except ImportError:
keystoneclient_found = False
else:
keystoneclient_found = True
import traceback
def authenticate(endpoint, token, login_user, login_password, tenant_name,
insecure):
"""Return a keystone client object"""
if token:
return client.Client(endpoint=endpoint, token=token, insecure=insecure)
else:
return client.Client(auth_url=endpoint, username=login_user,
password=login_password, tenant_name=tenant_name,
insecure=insecure)
def get_service(keystone, name):
""" Retrieve a service by name """
services = [x for x in keystone.services.list() if x.name == name]
count = len(services)
if count == 0:
raise KeyError("No keystone services with name %s" % name)
elif count > 1:
raise ValueError("%d services with name %s" % (count, name))
else:
return services[0]
def get_endpoint(keystone, name, region, ignore_other_regions):
""" Retrieve a service endpoint by name """
service = get_service(keystone, name)
endpoints = [x for x in keystone.endpoints.list()
if x.service_id == service.id]
# If this is a multi-region cloud only look at this region's endpoints
if ignore_other_regions:
endpoints = [x for x in endpoints if x.region == region]
count = len(endpoints)
if count == 0:
raise KeyError("No keystone endpoints with service name %s" % name)
elif count > 1:
raise ValueError("%d endpoints with service name %s" % (count, name))
else:
return endpoints[0]
def ensure_present(keystone, name, service_type, description, public_url,
internal_url, admin_url, region, ignore_other_regions,
check_mode):
""" Ensure the service and its endpoint are present and have the right values.
Returns a tuple, where the first element is a boolean that indicates
a state change, the second element is the service uuid (or None in
check mode), and the third element is the endpoint uuid (or None in
check mode)."""
# Fetch service and endpoint, if they exist.
service = None
endpoint = None
try: service = get_service(keystone, name)
except KeyError: pass
try: endpoint = get_endpoint(keystone, name, region, ignore_other_regions)
except KeyError: pass
changed = False
# Delete endpoint if it exists and doesn't match.
if endpoint is not None:
identical = endpoint.publicurl == public_url and \
endpoint.adminurl == admin_url and \
endpoint.internalurl == internal_url and \
endpoint.region == region
if not identical:
changed = True
ensure_endpoint_absent(keystone, name, check_mode, region,
ignore_other_regions)
endpoint = None
# Delete service and its endpoint if the service exists and doesn't match.
if service is not None:
identical = service.name == name and \
service.type == service_type and \
service.description == description
if not identical:
changed = True
ensure_endpoint_absent(keystone, name, check_mode, region,
ignore_other_regions)
endpoint = None
ensure_service_absent(keystone, name, check_mode)
service = None
# Recreate service, if necessary.
if service is None:
if not check_mode:
service = keystone.services.create(
name=name,
service_type=service_type,
description=description,
)
changed = True
# Recreate endpoint, if necessary.
if endpoint is None:
if not check_mode:
endpoint = keystone.endpoints.create(
region=region,
service_id=service.id,
publicurl=public_url,
adminurl=admin_url,
internalurl=internal_url,
)
changed = True
if check_mode:
# In check mode, the service/endpoint uuids will be the old uuids,
# so omit them.
return changed, None, None
return changed, service.id, endpoint.id
def ensure_service_absent(keystone, name, check_mode):
""" Ensure the service is absent"""
try:
service = get_service(keystone, name)
endpoints = [x for x in keystone.endpoints.list()
if x.service_id == service.id]
# Don't delete the service if it still has endpoints
if endpoints:
return False
if not check_mode:
keystone.services.delete(service.id)
return True
except KeyError:
# Service doesn't exist, so we're done.
return False
def ensure_endpoint_absent(keystone, name, check_mode, region,
ignore_other_regions):
""" Ensure the service endpoint """
try:
endpoint = get_endpoint(keystone, name, region, ignore_other_regions)
if not check_mode:
keystone.endpoints.delete(endpoint.id)
return True
except KeyError:
# Endpoint doesn't exist, so we're done.
return False
def dispatch(keystone, name, service_type, description, public_url,
internal_url, admin_url, region, ignore_other_regions, state,
check_mode):
if state == 'present':
(changed, service_id, endpoint_id) = ensure_present(
keystone,
name,
service_type,
description,
public_url,
internal_url,
admin_url,
region,
ignore_other_regions,
check_mode,
)
return dict(changed=changed, service_id=service_id, endpoint_id=endpoint_id)
elif state == 'absent':
endpoint_changed = ensure_endpoint_absent(keystone, name, check_mode,
region, ignore_other_regions)
service_changed = ensure_service_absent(keystone, name, check_mode)
return dict(changed=service_changed or endpoint_changed)
else:
raise ValueError("Code should never reach here")
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True),
type=dict(required=True),
description=dict(required=False),
public_url=dict(required=True, aliases=['url', 'publicurl']),
internal_url=dict(required=False, aliases=['internalurl']),
admin_url=dict(required=False, aliases=['adminurl']),
region=dict(required=False, default='RegionOne'),
ignore_other_regions=dict(required=False, default=False, type='bool'),
state=dict(default='present', choices=['present', 'absent']),
endpoint=dict(required=False,
default="http://127.0.0.1:35357/v2.0",
aliases=['auth_url']),
token=dict(required=False),
insecure=dict(required=False, default=False, type='bool'),
login_user=dict(required=False),
login_password=dict(required=False),
tenant_name=dict(required=False, aliases=['tenant'])
),
supports_check_mode=True,
mutually_exclusive=[['token', 'login_user'],
['token', 'login_password'],
['token', 'tenant_name']]
)
endpoint = module.params['endpoint']
token = module.params['token']
login_user = module.params['login_user']
login_password = module.params['login_password']
tenant_name = module.params['tenant_name']
insecure = module.boolean(module.params['insecure'])
name = module.params['name']
service_type = module.params['type']
description = module.params['description']
public_url = module.params['public_url']
internal_url = module.params['internal_url']
if internal_url is None:
internal_url = public_url
admin_url = module.params['admin_url']
if admin_url is None:
admin_url = public_url
region = module.params['region']
ignore_other_regions = module.boolean(module.params['ignore_other_regions'])
state = module.params['state']
keystone = authenticate(endpoint, token, login_user, login_password,
tenant_name, insecure)
check_mode = module.check_mode
try:
d = dispatch(keystone, name, service_type, description,
public_url, internal_url, admin_url, region,
ignore_other_regions, state, check_mode)
except Exception:
if check_mode:
# If we have a failure in check mode
module.exit_json(changed=True,
msg="exception: %s" % traceback.format_exc())
else:
module.fail_json(msg=traceback.format_exc())
else:
module.exit_json(**d)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
if __name__ == '__main__':
main()
|