Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2017 Fred Sun, <[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_automationrunbook
14 version_added: '1.12.0'
15 short_description: Mange automation runbook
16 description:
17 - Create, update, delete or publish automation runbook.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 required: True
25 name:
26 description:
27 - The name of the automation runbook.
28 type: str
29 required: True
30 automation_account_name:
31 description:
32 - The name of the automation account.
33 type: str
34 required: True
35 location:
36 description:
37 - The location of the automation runbook.
38 type: str
39 description:
40 description:
41 - Sets the description.
42 type: str
43 runbook_type:
44 description:
45 - Sets the type of the runbook.
46 type: str
47 choices:
48 - Script
49 - Graph
50 - PowerShellWorkflow
51 - PowerShell
52 - GraphPowerShellWorkflow
53 - GraphPowerShell
54 log_activity_trace:
55 description:
56 - Sets the option to log activity trace of the runbook.
57 type: int
58 log_progress:
59 description:
60 - Sets progress log option.
61 type: bool
62 log_verbose:
63 description:
64 - Sets verbose log option.
65 type: bool
66 publish:
67 description:
68 - Whether to publish the runbook.
69 type: bool
70 state:
71 description:
72 - State of the automation runbook. Use C(present) to create or update a automation runbook and use C(absent) to delete.
73 type: str
74 default: present
75 choices:
76 - present
77 - absent
78
79 extends_documentation_fragment:
80 - azure.azcollection.azure
81 - azure.azcollection.azure_tags
82
83 author:
84 - Fred Sun (@Fred-sun)
85
86 '''
87
88 EXAMPLES = '''
89 - name: create automation runbook with default parameters
90 azure_rm_automationrunbook:
91 resource_group: "{{ resource_group }}"
92 automation_account_name: "{{ account-name }}"
93 name: "{{ runbook-name }}"
94 runbook_type: "Script"
95 description: "Fred test"
96
97 - name: create automation runbook with more parameters
98 azure_rm_automationrunbook:
99 resource_group: "{{ resource_group }}"
100 automation_account_name: "{{ account-name }}"
101 name: "{{ runbook-name }}"
102 runbook_type: "Script"
103 description: "Fred test"
104 log_activity_trace: 3
105 log_progress: true
106 log_verbose: false
107 tags:
108 key1: value1
109
110 - name: Publish automation runbook
111 azure_rm_automationrunbook:
112 resource_group: "{{ resource_group }}"
113 automation_account_name: "{{ account-name }}"
114 name: "{{ runbook-name }}"
115 publish: true
116
117 - name: Delete automation runbook
118 azure_rm_automationrunbook:
119 resource_group: "{{ resource_group }}"
120 automation_account_name: "{{ account-name }}"
121 name: "{{ runbook-name }}"
122 state: absent
123 '''
124
125 RETURN = '''
126 state:
127 description:
128 - List of automation runbook dicts.
129 returned: always
130 type: complex
131 contains:
132 id:
133 description:
134 - Resource ID.
135 type: str
136 returned: always
137 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups
138 /myResourceGroup/providers/Microsoft.Automation/automationAccounts/test/runbooks/runbook_name"
139 resource_group:
140 description:
141 - Resource group name.
142 type: str
143 returned: always
144 sample: myResourceGroup
145 name:
146 description:
147 - Resource name.
148 type: str
149 returned: always
150 sample: Testing
151 location:
152 description:
153 - Resource location.
154 type: str
155 returned: always
156 sample: eastus
157 creation_time:
158 description:
159 - The resource creation date time.
160 type: str
161 returned: always
162 sample: "2022-03-24T06:30:54.116666+00:00"
163 job_count:
164 description:
165 - The job count of the runbook.
166 type: int
167 returned: always
168 sample: 3
169 last_modified_by:
170 description:
171 - The resource last modifier.
172 type: str
173 returned: always
174 sample: Fred-sun
175 last_modified_time:
176 description:
177 - The last person to update the resource.
178 type: str
179 returned: always
180 sample: "2022-03-25T06:30:54.116666+00:00"
181 log_activity_trace:
182 description:
183 - The option to log activity trace of the runbook.
184 type: int
185 returned: always
186 sample: 3
187 log_progress:
188 description:
189 - Whether show progress log option.
190 type: bool
191 returned: always
192 sample: True
193 log_verbose:
194 description:
195 - Whether show verbose log option.
196 type: bool
197 returned: always
198 sample: True
199 output_types:
200 description:
201 - The runbook output type.
202 type: list
203 returned: always
204 sample: []
205 runbook_content_link:
206 description:
207 - The publish runbook content link.
208 type: str
209 returned: always
210 sample: null
211 state:
212 description:
213 - The resource state.
214 type: str
215 returned: always
216 sample: Published
217 tags:
218 description:
219 - The resource tags.
220 type: dict
221 returned: always
222 sample: { 'key1': 'value1' }
223 type:
224 description:
225 - The resource automation runbook type.
226 type: str
227 returned: always
228 sample: "Microsoft.Automation/AutomationAccounts/Runbooks"
229 '''
230
231 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
232
233 try:
234 from azure.core.exceptions import ResourceNotFoundError
235 except ImportError:
236 pass
237
238
239 class AzureRMAutomationRunbook(AzureRMModuleBase):
240 def __init__(self):
241 # define user inputs into argument
242 self.module_arg_spec = dict(
243 resource_group=dict(
244 type='str',
245 required=True
246 ),
247 name=dict(
248 type='str',
249 required=True
250 ),
251 automation_account_name=dict(
252 type='str',
253 required=True
254 ),
255 runbook_type=dict(
256 type='str',
257 choices=['Script', 'Graph', 'PowerShellWorkflow', 'PowerShell', 'GraphPowerShellWorkflow', 'GraphPowerShell']
258 ),
259 description=dict(
260 type='str'
261 ),
262 location=dict(
263 type='str'
264 ),
265 log_activity_trace=dict(
266 type='int'
267 ),
268 log_progress=dict(
269 type='bool'
270 ),
271 publish=dict(
272 type='bool'
273 ),
274 log_verbose=dict(
275 type='bool'
276 ),
277 state=dict(
278 type='str',
279 choices=['present', 'absent'],
280 default='present'
281 )
282 )
283 # store the results of the module operation
284 self.results = dict()
285 self.resource_group = None
286 self.name = None
287 self.automation_account_name = None
288 self.runbook_type = None
289 self.description = None
290 self.log_activity_trace = None
291 self.log_progress = None
292 self.log_verbose = None
293 self.location = None
294 self.publish = None
295
296 super(AzureRMAutomationRunbook, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=True)
297
298 def exec_module(self, **kwargs):
299
300 for key in list(self.module_arg_spec) + ['tags']:
301 setattr(self, key, kwargs[key])
302
303 if not self.location:
304 resource_group = self.get_resource_group(self.resource_group)
305 self.location = resource_group.location
306
307 runbook = self.get()
308 changed = False
309
310 if self.state == 'present':
311 if runbook:
312 update_parameter = dict()
313 if self.tags is not None:
314 update_tags, tags = self.update_tags(runbook['tags'])
315 if update_tags:
316 changed = True
317 update_parameter['tags'] = tags
318 if self.description is not None and self.description != runbook['description']:
319 changed = True
320 update_parameter['description'] = self.description
321 if self.log_activity_trace is not None and self.log_activity_trace != runbook['log_activity_trace']:
322 changed = True
323 update_parameter['log_activity_trace'] = self.log_activity_trace
324 if self.log_progress is not None and self.log_progress != runbook['log_progress']:
325 changed = True
326 update_parameter['log_progress'] = self.log_progress
327 if self.log_verbose is not None and self.log_verbose != runbook['log_verbose']:
328 changed = True
329 update_parameter['log_verbose'] = self.log_verbose
330 if self.location is not None and self.location != runbook['location']:
331 changed = True
332 self.fail("Parameter error (location): The parameters {0} cannot be update".format(self.location))
333 if self.runbook_type is not None and self.runbook_type != runbook['runbook_type']:
334 changed = True
335 self.fail("Parameter error (runbook_type): The parameters {0} cannot be update".format(self.runbook_type))
336
337 if changed:
338 if not self.check_mode:
339 if update_parameter.get('log_activity_trace'):
340 runbook['log_activity_trace'] = update_parameter.get('log_activity_trace')
341
342 paramters = self.automation_models.RunbookCreateOrUpdateParameters(
343 location=runbook['location'] if update_parameter.get('location') else update_parameter.get('location'),
344 log_verbose=runbook['log_verbose'] if update_parameter.get('log_verbose') else update_parameter.get('log_verbose'),
345 runbook_type=runbook['runbook_type'] if update_parameter.get('runbook_type') else update_parameter.get('runbook_type'),
346 description=runbook['description'] if update_parameter.get('description') else update_parameter.get('description'),
347 log_activity_trace=runbook['log_activity_trace'],
348 tags=runbook['tags'] if update_parameter.get('tags') else update_parameter.get('tags'),
349 log_progress=runbook['log_progress'] if update_parameter.get('log_progress') else update_parameter.get('log_progress')
350 )
351
352 runbook = self.update_runbook(update_parameter)
353
354 else:
355 paramters = self.automation_models.RunbookCreateOrUpdateParameters(
356 location=self.location,
357 log_verbose=self.log_verbose,
358 runbook_type=self.runbook_type,
359 description=self.description,
360 log_activity_trace=self.log_activity_trace,
361 tags=self.tags,
362 log_progress=self.log_progress
363 )
364 changed = True
365 if not self.check_mode:
366 runbook = self.create_or_update(paramters)
367
368 if not self.check_mode:
369 if self.publish and runbook['state'] != 'Published':
370 changed = True
371 self.publish_runbook()
372 else:
373 changed = True
374 if not self.check_mode:
375 runbook = self.delete_automation_runbook()
376
377 self.results['changed'] = changed
378 self.results['state'] = runbook
379 return self.results
380
381 def get(self):
382 try:
383 response = self.automation_client.runbook.get(self.resource_group, self.automation_account_name, self.name)
384 return self.to_dict(response)
385 except ResourceNotFoundError:
386 pass
387
388 def publish_runbook(self):
389 response = None
390 try:
391 response = self.automation_client.runbook.begin_publish(self.resource_group, self.automation_account_name, self.name)
392 except Exception as exc:
393 self.fail('Error when updating automation account {0}: {1}'.format(self.name, exc.message))
394
395 def update_runbook(self, parameters):
396 try:
397 response = self.automation_client.runbook.update(self.resource_group, self.automation_account_name, self.name, parameters)
398 return self.to_dict(response)
399 except Exception as exc:
400 self.fail('Error when updating automation account {0}: {1}'.format(self.name, exc.message))
401
402 def create_or_update(self, parameters):
403 try:
404 response = self.automation_client.runbook.create_or_update(self.resource_group, self.automation_account_name, self.name, parameters)
405 return self.to_dict(response)
406 except Exception as exc:
407 self.fail('Error when creating automation account {0}: {1}'.format(self.name, exc.message))
408
409 def delete_automation_runbook(self):
410 try:
411 return self.automation_client.runbook.delete(self.resource_group, self.automation_account_name, self.name)
412 except Exception as exc:
413 self.fail('Error when deleting automation account {0}: {1}'.format(self.name, exc.message))
414
415 def to_dict(self, runbook):
416 if not runbook:
417 return None
418 runbook_dict = dict(
419 id=runbook.id,
420 type=runbook.type,
421 name=runbook.name,
422 tags=runbook.tags,
423 location=runbook.location,
424 runbook_type=runbook.runbook_type,
425 runbook_content_link=runbook.publish_content_link,
426 state=runbook.state,
427 log_verbose=runbook.log_verbose,
428 log_progress=runbook.log_progress,
429 log_activity_trace=runbook.log_activity_trace,
430 job_count=runbook.job_count,
431 output_types=runbook.output_types,
432 last_modified_by=runbook.last_modified_by,
433 last_modified_time=runbook.last_modified_time,
434 creation_time=runbook.creation_time,
435 description=runbook.description
436 )
437 return runbook_dict
438
439
440 def main():
441 AzureRMAutomationRunbook()
442
443
444 if __name__ == '__main__':
445 main()