Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualmachineextension.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2017 Sertac Ozercan <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_virtualmachineextension
14
15 version_added: "0.1.2"
16
17 short_description: Managed Azure Virtual Machine extension
18
19 description:
20 - Create, update and delete Azure Virtual Machine Extension.
21 - Note that this module was called M(azure.azcollection.azure_rm_virtualmachine_extension) before Ansible 2.8. The usage did not change.
22
23 options:
24 resource_group:
25 description:
26 - Name of a resource group where the vm extension exists or will be created.
27 required: true
28 type: str
29 name:
30 description:
31 - Name of the vm extension.
32 required: true
33 type: str
34 state:
35 description:
36 - State of the vm extension. Use C(present) to create or update a vm extension and C(absent) to delete a vm extension.
37 default: present
38 type: str
39 choices:
40 - absent
41 - present
42 location:
43 description:
44 - Valid Azure location. Defaults to location of the resource group.
45 type: str
46 virtual_machine_name:
47 description:
48 - The name of the virtual machine where the extension should be create or updated.
49 required: true
50 type: str
51 publisher:
52 description:
53 - The name of the extension handler publisher.
54 type: str
55 virtual_machine_extension_type:
56 description:
57 - The type of the extension handler.
58 type: str
59 type_handler_version:
60 description:
61 - The type version of the extension handler.
62 type: str
63 settings:
64 description:
65 - JSON formatted public settings for the extension.
66 type: dict
67 protected_settings:
68 description:
69 - JSON formatted protected settings for the extension.
70 - >-
71 Previously configured settings are not available, so the parameter is not used for idempotency checks.
72 If changes to this parameter need to be applied, use in conjunction with I(force_update_tag).
73 type: dict
74 auto_upgrade_minor_version:
75 description:
76 - Whether the extension handler should be automatically upgraded across minor versions during deployment.
77 type: bool
78 enable_automatic_upgrade:
79 description:
80 - Whether the extension handler should be automatically upgraded.
81 type: bool
82 force_update_tag:
83 description:
84 - Whether the extension should be updated or re-run even if no changes can be detected from what is currently configured.
85 - Helpful when applying changes to I(protected_settings).
86 type: bool
87 default: false
88 version_added: '1.10.0'
89
90 extends_documentation_fragment:
91 - azure.azcollection.azure
92
93 author:
94 - Sertac Ozercan (@sozercan)
95 - Julien Stroheker (@julienstroheker)
96 '''
97
98 EXAMPLES = '''
99 - name: Create VM Extension
100 azure_rm_virtualmachineextension:
101 name: myvmextension
102 location: eastus
103 resource_group: myResourceGroup
104 virtual_machine_name: myvm
105 publisher: Microsoft.Azure.Extensions
106 virtual_machine_extension_type: CustomScript
107 type_handler_version: 2.0
108 settings: '{"commandToExecute": "hostname"}'
109 auto_upgrade_minor_version: true
110
111 - name: Delete VM Extension
112 azure_rm_virtualmachineextension:
113 name: myvmextension
114 resource_group: myResourceGroup
115 virtual_machine_name: myvm
116 state: absent
117 '''
118
119 RETURN = '''
120 state:
121 description:
122 - Current state of the vm extension.
123 returned: always
124 type: dict
125 sample: { "state":"Deleted" }
126
127 changed:
128 description:
129 - Whether or not the resource has changed.
130 returned: always
131 type: bool
132 sample: true
133 '''
134
135 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
136
137 try:
138 from azure.core.exceptions import ResourceNotFoundError
139 except ImportError:
140 # This is handled in azure_rm_common
141 pass
142
143
144 def vmextension_to_dict(extension):
145 '''
146 Serializing the VM Extension from the API to Dict
147 :return: dict
148 '''
149 return dict(
150 id=extension.id,
151 name=extension.name,
152 location=extension.location,
153 publisher=extension.publisher,
154 virtual_machine_extension_type=extension.type_properties_type,
155 type_handler_version=extension.type_handler_version,
156 auto_upgrade_minor_version=extension.auto_upgrade_minor_version,
157 enable_automatic_upgrade=extension.enable_automatic_upgrade,
158 settings=extension.settings,
159 protected_settings=extension.protected_settings,
160 )
161
162
163 class AzureRMVMExtension(AzureRMModuleBase):
164 """Configuration class for an Azure RM VM Extension resource"""
165
166 def __init__(self):
167 self.module_arg_spec = dict(
168 resource_group=dict(
169 type='str',
170 required=True
171 ),
172 name=dict(
173 type='str',
174 required=True
175 ),
176 state=dict(
177 type='str',
178 default='present',
179 choices=['present', 'absent']
180 ),
181 location=dict(
182 type='str'
183 ),
184 virtual_machine_name=dict(
185 type='str',
186 required=True
187 ),
188 publisher=dict(
189 type='str'
190 ),
191 virtual_machine_extension_type=dict(
192 type='str'
193 ),
194 type_handler_version=dict(
195 type='str'
196 ),
197 auto_upgrade_minor_version=dict(
198 type='bool'
199 ),
200 enable_automatic_upgrade=dict(
201 type='bool'
202 ),
203 settings=dict(
204 type='dict'
205 ),
206 protected_settings=dict(
207 type='dict', no_log=True
208 ),
209 force_update_tag=dict(
210 type='bool',
211 default=False
212 ),
213 )
214
215 self.resource_group = None
216 self.name = None
217 self.virtual_machine_name = None
218 self.location = None
219 self.publisher = None
220 self.virtual_machine_extension_type = None
221 self.type_handler_version = None
222 self.auto_upgrade_minor_version = None
223 self.enable_automatic_upgrade = None
224 self.settings = None
225 self.protected_settings = None
226 self.state = None
227 self.force_update_tag = False
228
229 required_if = [
230 ('state', 'present', ['publisher', 'virtual_machine_extension_type', 'type_handler_version']),
231 ]
232
233 self.results = dict(changed=False, state=dict())
234
235 super(AzureRMVMExtension, self).__init__(derived_arg_spec=self.module_arg_spec,
236 supports_check_mode=False,
237 supports_tags=False,
238 required_if=required_if)
239
240 def exec_module(self, **kwargs):
241 """Main module execution method"""
242
243 for key in list(self.module_arg_spec.keys()):
244 setattr(self, key, kwargs[key])
245
246 resource_group = None
247 to_be_updated = False
248
249 resource_group = self.get_resource_group(self.resource_group)
250 if not self.location:
251 self.location = resource_group.location
252
253 response = self.get_vmextension()
254
255 if self.state == 'present':
256 if not response:
257 to_be_updated = True
258 else:
259 if self.force_update_tag:
260 to_be_updated = True
261
262 if self.settings is not None:
263 if response['settings'] != self.settings:
264 response['settings'] = self.settings
265 to_be_updated = True
266 else:
267 self.settings = response['settings']
268
269 if response['location'] != self.location:
270 self.location = response['location']
271 self.module.warn("Property 'location' cannot be changed")
272
273 if response['publisher'] != self.publisher:
274 self.publisher = response['publisher']
275 self.module.warn("Property 'publisher' cannot be changed")
276
277 if response['virtual_machine_extension_type'] != self.virtual_machine_extension_type:
278 self.virtual_machine_extension_type = response['virtual_machine_extension_type']
279 self.module.warn("Property 'virtual_machine_extension_type' cannot be changed")
280
281 if response['type_handler_version'] != self.type_handler_version:
282 response['type_handler_version'] = self.type_handler_version
283 to_be_updated = True
284
285 if self.auto_upgrade_minor_version is not None:
286 if response['auto_upgrade_minor_version'] != self.auto_upgrade_minor_version:
287 response['auto_upgrade_minor_version'] = self.auto_upgrade_minor_version
288 to_be_updated = True
289 else:
290 self.auto_upgrade_minor_version = response['auto_upgrade_minor_version']
291
292 if self.auto_upgrade_minor_version is not None:
293 if response['enable_automatic_upgrade'] != self.enable_automatic_upgrade:
294 response['enable_automatic_upgrade'] = self.enable_automatic_upgrade
295 to_be_updated = True
296 else:
297 self.enable_automatic_upgrade = response['enable_automatic_upgrade']
298
299 if to_be_updated:
300 self.results['changed'] = True
301 self.results['state'] = self.create_or_update_vmextension()
302 elif self.state == 'absent':
303 if response:
304 self.delete_vmextension()
305 self.results['changed'] = True
306
307 return self.results
308
309 def create_or_update_vmextension(self):
310 '''
311 Method calling the Azure SDK to create or update the VM extension.
312 :return: void
313 '''
314 self.log("Creating VM extension {0}".format(self.name))
315 try:
316 params = self.compute_models.VirtualMachineExtension(
317 location=self.location,
318 publisher=self.publisher,
319 type_properties_type=self.virtual_machine_extension_type,
320 type_handler_version=self.type_handler_version,
321 auto_upgrade_minor_version=self.auto_upgrade_minor_version,
322 enable_automatic_upgrade=self.enable_automatic_upgrade,
323 settings=self.settings,
324 protected_settings=self.protected_settings,
325 force_update_tag=self.force_update_tag,
326 )
327 poller = self.compute_client.virtual_machine_extensions.begin_create_or_update(self.resource_group, self.virtual_machine_name, self.name, params)
328 response = self.get_poller_result(poller)
329 return vmextension_to_dict(response)
330
331 except Exception as e:
332 self.log('Error attempting to create the VM extension.')
333 self.fail("Error creating the VM extension: {0}".format(str(e)))
334
335 def delete_vmextension(self):
336 '''
337 Method calling the Azure SDK to delete the VM Extension.
338 :return: void
339 '''
340 self.log("Deleting vmextension {0}".format(self.name))
341 try:
342 poller = self.compute_client.virtual_machine_extensions.begin_delete(self.resource_group, self.virtual_machine_name, self.name)
343 self.get_poller_result(poller)
344 except Exception as e:
345 self.log('Error attempting to delete the vmextension.')
346 self.fail("Error deleting the vmextension: {0}".format(str(e)))
347
348 def get_vmextension(self):
349 '''
350 Method calling the Azure SDK to get a VM Extension.
351 :return: void
352 '''
353 self.log("Checking if the vm extension {0} is present".format(self.name))
354 found = False
355 try:
356 response = self.compute_client.virtual_machine_extensions.get(self.resource_group, self.virtual_machine_name, self.name)
357 found = True
358 except ResourceNotFoundError as e:
359 self.log('Did not find vm extension')
360 if found:
361 return vmextension_to_dict(response)
362 else:
363 return False
364
365
366 def main():
367 """Main execution"""
368 AzureRMVMExtension()
369
370
371 if __name__ == '__main__':
372 main()