Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_automationrunbook_info.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_info
14 version_added: '1.12.0'
15 short_description: Get Azure automation runbook facts
16 description:
17 - Get facts of automation runbook.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 required: True
25 automation_account_name:
26 description:
27 - The name of the automation account.
28 type: str
29 required: True
30 name:
31 description:
32 - The name of the automation runbook.
33 type: str
34 show_content:
35 description:
36 - Wether retrieve the content of runbook identified by runbook name.
37 type: bool
38 tags:
39 description:
40 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
41 type: list
42 elements: str
43
44 extends_documentation_fragment:
45 - azure.azcollection.azure
46
47 author:
48 - Fred Sun (@Fred-sun)
49
50 '''
51
52 EXAMPLES = '''
53 - name: Get details of an automation account
54 azure_rm_automationrunbook_info:
55 resource_group: "{{ resource_group }}"
56 automation_account_name: "{{ account-name }}"
57 name: "{{ runbook-name }}"
58
59 - name: List automation runbook in the account
60 azure_rm_automationrunbook_info:
61 resource_group: "{{ resource_group }}"
62 automation_account_name: "{{ account-name }}"
63
64 - name: Get details of an automation account
65 azure_rm_automationrunbook_info:
66 resource_group: "{{ resource_group }}"
67 automation_account_name: "{{ account-name }}"
68 name: "{{ runbook-name }}"
69 show_content: true
70 '''
71
72 RETURN = '''
73 automation_runbook:
74 description:
75 - List of automation runbook dicts.
76 returned: always
77 type: complex
78 contains:
79 id:
80 description:
81 - Resource ID.
82 type: str
83 returned: always
84 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups
85 /myResourceGroup/providers/Microsoft.Automation/automationAccounts/test/runbooks/runbook_name"
86 resource_group:
87 description:
88 - Resource group name.
89 type: str
90 returned: always
91 sample: myResourceGroup
92 name:
93 description:
94 - Resource name.
95 type: str
96 returned: always
97 sample: Testing
98 location:
99 description:
100 - Resource location.
101 type: str
102 returned: always
103 sample: eastus
104 creation_time:
105 description:
106 - The resource creation date time.
107 type: str
108 returned: always
109 sample: "2022-03-24T06:30:54.116666+00:00"
110 job_count:
111 description:
112 - The job count of the runbook.
113 type: int
114 returned: always
115 sample: 3
116 last_modified_by:
117 description:
118 - The resource last modifier.
119 type: str
120 returned: always
121 sample: Fred-sun
122 last_modified_time:
123 description:
124 - The last person to update the resource.
125 type: str
126 returned: always
127 sample: "2022-03-25T06:30:54.116666+00:00"
128 log_activity_trace:
129 description:
130 - The option to log activity trace of the runbook.
131 type: int
132 returned: always
133 sample: 3
134 log_progress:
135 description:
136 - Whether show progress log option.
137 type: bool
138 returned: always
139 sample: True
140 log_verbose:
141 description:
142 - Whether show verbose log option.
143 type: bool
144 returned: always
145 sample: True
146 output_types:
147 description:
148 - The runbook output type.
149 type: list
150 returned: always
151 sample: []
152 runbook_content_link:
153 description:
154 - The publish runbook content link.
155 type: str
156 returned: always
157 sample: null
158 state:
159 description:
160 - The resource state.
161 type: str
162 returned: always
163 sample: Published
164 tags:
165 description:
166 - The resource tags.
167 type: list
168 returned: always
169 sample: { 'key1': 'value1' }
170 type:
171 description:
172 - The resource automation runbook type.
173 type: str
174 returned: always
175 sample: "Microsoft.Automation/AutomationAccounts/Runbooks"
176 '''
177
178 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
179
180 try:
181 from azure.core.exceptions import ResourceNotFoundError
182 except ImportError:
183 pass
184
185
186 class AzureRMAutomationRunbookInfo(AzureRMModuleBase):
187 def __init__(self):
188 # define user inputs into argument
189 self.module_arg_spec = dict(
190 resource_group=dict(
191 type='str',
192 required=True
193 ),
194 name=dict(
195 type='str'
196 ),
197 automation_account_name=dict(
198 type='str',
199 required=True
200 ),
201 show_content=dict(
202 type='bool'
203 ),
204 tags=dict(
205 type='list',
206 elements='str'
207 )
208 )
209 # store the results of the module operation
210 self.results = dict()
211 self.resource_group = None
212 self.name = None
213 self.tags = None
214 self.automation_account_name = None
215 self.show_content = None
216
217 super(AzureRMAutomationRunbookInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
218
219 def exec_module(self, **kwargs):
220
221 for key in list(self.module_arg_spec) + ['tags']:
222 setattr(self, key, kwargs[key])
223
224 if self.name and self.show_content:
225 runbooks = [self.get_content()]
226 elif self.name:
227 runbooks = [self.get()]
228 else:
229 runbooks = self.list_by_automaiton_account()
230 self.results['automation_runbook'] = [self.to_dict(x) for x in runbooks if x and self.has_tags(x.tags, self.tags)]
231 return self.results
232
233 def get_content(self):
234 try:
235 return self.automation_client.runbook.get(self.resource_group, self.automation_account_name, self.name)
236 except ResourceNotFoundError as exc:
237 pass
238
239 def get(self):
240 try:
241 return self.automation_client.runbook.get(self.resource_group, self.automation_account_name, self.name)
242 except ResourceNotFoundError as exc:
243 pass
244
245 def list_by_automaiton_account(self):
246 result = []
247 try:
248 resp = self.automation_client.runbook.list_by_automation_account(self.resource_group, self.automation_account_name)
249 while True:
250 result.append(resp.next())
251 except StopIteration:
252 pass
253 except Exception as exc:
254 pass
255 return result
256
257 def to_dict(self, runbook):
258 if not runbook:
259 return None
260 runbook_dict = dict(
261 id=runbook.id,
262 type=runbook.type,
263 name=runbook.name,
264 tags=runbook.tags,
265 location=runbook.location,
266 runbook_type=runbook.runbook_type,
267 runbook_content_link=runbook.publish_content_link,
268 state=runbook.state,
269 log_verbose=runbook.log_verbose,
270 log_progress=runbook.log_progress,
271 log_activity_trace=runbook.log_activity_trace,
272 job_count=runbook.job_count,
273 output_types=runbook.output_types,
274 last_modified_by=runbook.last_modified_by,
275 last_modified_time=runbook.last_modified_time,
276 creation_time=runbook.creation_time,
277 description=runbook.description
278 )
279
280 return runbook_dict
281
282
283 def main():
284 AzureRMAutomationRunbookInfo()
285
286
287 if __name__ == '__main__':
288 main()