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
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2016 Jakub Jursa <jakub.jursa1@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = '''
---
module: host_aggregate
short_description: Manage OpenStack host aggregates
author: OpenStack Ansible SIG
description:
- Create, update, or delete OpenStack host aggregates. If a aggregate
with the supplied name already exists, it will be updated with the
new name, new availability zone, new metadata and new list of hosts.
options:
name:
description: Name of the aggregate.
required: true
type: str
metadata:
description: Metadata dict.
type: dict
availability_zone:
description: Availability zone to create aggregate into.
type: str
hosts:
description: List of hosts to set for an aggregate.
type: list
elements: str
purge_hosts:
description: Whether hosts not in I(hosts) should be removed from the aggregate
type: bool
default: true
state:
description: Should the resource be present or absent.
choices: [present, absent]
default: present
type: str
extends_documentation_fragment:
- openstack.cloud.openstack
'''
EXAMPLES = '''
# Create a host aggregate
- openstack.cloud.host_aggregate:
cloud: mycloud
state: present
name: db_aggregate
hosts:
- host1
- host2
metadata:
type: dbcluster
# Add an additional host to the aggregate
- openstack.cloud.host_aggregate:
cloud: mycloud
state: present
name: db_aggregate
hosts:
- host3
purge_hosts: false
metadata:
type: dbcluster
# Delete an aggregate
- openstack.cloud.host_aggregate:
cloud: mycloud
state: absent
name: db_aggregate
'''
RETURN = r'''
aggregate:
description: A host aggregate resource.
type: dict
returned: On success, when I(state) is present
contains:
availability_zone:
description: Availability zone of the aggregate
type: str
returned: always
created_at:
description: The date and time when the resource was created
type: str
returned: always
deleted_at:
description:
- The date and time when the resource was deleted
- Null unless I(is_deleted) is true
type: str
returned: always
hosts:
description: Hosts belonging to the aggregate
type: str
returned: always
id:
description: The UUID of the aggregate.
type: str
returned: always
is_deleted:
description: Whether or not the resource is deleted
type: bool
returned: always
metadata:
description: Metadata attached to the aggregate
type: str
returned: always
name:
description: Name of the aggregate
type: str
returned: always
updated_at:
description: The date and time when the resource was updated
type: str
returned: always
uuid:
description: UUID of the aggregate
type: str
returned: always
'''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
class ComputeHostAggregateModule(OpenStackModule):
argument_spec = dict(
name=dict(required=True),
metadata=dict(type='dict'),
availability_zone=dict(),
hosts=dict(type='list', elements='str'),
purge_hosts=dict(default=True, type='bool'),
state=dict(default='present', choices=['absent', 'present']),
)
module_kwargs = dict(
supports_check_mode=True
)
def _needs_update(self, aggregate):
new_metadata = self.params['metadata'] or {}
if self.params['availability_zone'] is not None:
new_metadata['availability_zone'] = self.params['availability_zone']
if self.params['hosts'] is not None:
if self.params['purge_hosts']:
if set(self.params['hosts']) != set(aggregate.hosts):
return True
else:
intersection = set(self.params['hosts']).intersection(set(aggregate.hosts))
if set(self.params['hosts']) != intersection:
return True
for param in ('availability_zone', 'metadata'):
if self.params[param] is not None and \
self.params[param] != aggregate[param]:
return True
return False
def _system_state_change(self, aggregate):
state = self.params['state']
if state == 'absent' and aggregate:
return True
if state == 'present':
if aggregate is None:
return True
return self._needs_update(aggregate)
return False
def _update_hosts(self, aggregate, hosts, purge_hosts):
if hosts is None:
return
hosts_to_add = set(hosts) - set(aggregate['hosts'] or [])
for host in hosts_to_add:
self.conn.compute.add_host_to_aggregate(aggregate.id, host)
if not purge_hosts:
return
hosts_to_remove = set(aggregate["hosts"] or []) - set(hosts)
for host in hosts_to_remove:
self.conn.compute.remove_host_from_aggregate(aggregate.id, host)
def run(self):
name = self.params['name']
metadata = self.params['metadata']
availability_zone = self.params['availability_zone']
hosts = self.params['hosts']
purge_hosts = self.params['purge_hosts']
state = self.params['state']
if metadata is not None:
metadata.pop('availability_zone', None)
aggregate = self.conn.compute.find_aggregate(name)
if self.ansible.check_mode:
self.exit_json(changed=self._system_state_change(aggregate))
changed = False
if state == 'present':
if aggregate is None:
aggregate = self.conn.compute.create_aggregate(
name=name, availability_zone=availability_zone)
self._update_hosts(aggregate, hosts, False)
if metadata:
self.conn.compute.set_aggregate_metadata(aggregate, metadata)
changed = True
elif self._needs_update(aggregate):
if availability_zone is not None:
aggregate = self.conn.compute.update_aggregate(
aggregate, name=name,
availability_zone=availability_zone)
if metadata is not None:
metas = metadata
for i in set(aggregate.metadata.keys() - set(metadata.keys())):
if i != 'availability_zone':
metas[i] = None
self.conn.compute.set_aggregate_metadata(aggregate, metas)
self._update_hosts(aggregate, hosts, purge_hosts)
changed = True
aggregate = self.conn.compute.find_aggregate(name)
if aggregate:
aggregate = aggregate.to_dict(computed=False)
self.exit_json(changed=changed, aggregate=aggregate)
elif state == 'absent' and aggregate is not None:
self._update_hosts(aggregate, [], True)
self.conn.compute.delete_aggregate(aggregate.id)
changed = True
self.exit_json(changed=changed)
def main():
module = ComputeHostAggregateModule()
module()
if __name__ == '__main__':
main()
|