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
|
#!/usr/bin/python
#
# Copyright (c) 2017 Yuwei Zhou, <yuwzho@microsoft.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: azure_rm_automationaccount
version_added: "0.1.2"
short_description: Manage Azure Automation account
description:
- Create, delete an Azure Automation account.
options:
resource_group:
description:
- Name of resource group.
type: str
required: true
name:
description:
- Name of the automation account.
type: str
required: true
state:
description:
- State of the automation account. Use C(present) to create or update a automation account and C(absent) to delete an automation account.
type: str
default: present
choices:
- absent
- present
location:
description:
- Location of the resource.
- If not specified, use resource group location.
type: str
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Yuwei Zhou (@yuwzho)
'''
EXAMPLES = '''
- name: Create an automation account
azure_rm_automationaccount:
name: Testing
resource_group: myResourceGroup
- name: Create an automation account
azure_rm_automationaccount:
name: Testing
resource_group: myResourceGroup
location: eastus
'''
RETURN = '''
id:
description:
- Automation account resource ID.
type: str
returned: success
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Automation/automationAccounts/Testing"
''' # NOQA
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
try:
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
pass
class AzureRMAutomationAccount(AzureRMModuleBase):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(type='str', required=True),
name=dict(type='str', required=True),
state=dict(type='str', default='present', choices=['present', 'absent']),
location=dict(type='str')
)
self.results = dict(
changed=False,
id=None
)
self.resource_group = None
self.name = None
self.state = None
self.location = None
super(AzureRMAutomationAccount, self).__init__(self.module_arg_spec, supports_check_mode=True)
def exec_module(self, **kwargs):
for key in list(self.module_arg_spec.keys()) + ['tags']:
setattr(self, key, kwargs[key])
account = self.get_account()
changed = False
if self.state == 'present':
if not account:
if not self.location:
resource_group = self.get_resource_group(self.resource_group)
self.location = resource_group.location
param = self.automation_models.AutomationAccountCreateOrUpdateParameters(
location=self.location,
sku=self.automation_models.Sku(name='Basic'),
tags=self.tags
)
changed = True
if not self.check_mode:
account = self.create_or_update(param)
elif self.tags:
update_tags, tags = self.update_tags(account.tags)
if update_tags:
changed = True
param = self.automation_models.AutomationAccountUpdateParameters(
tags=tags
)
changed = True
if not self.check_mode:
self.update_account_tags(param)
if account:
self.results['id'] = account.id
elif account:
changed = True
if not self.check_mode:
self.delete_account()
self.results['changed'] = changed
return self.results
def get_account(self):
try:
return self.automation_client.automation_account.get(self.resource_group, self.name)
except ResourceNotFoundError:
pass
def create_or_update(self, param):
try:
return self.automation_client.automation_account.create_or_update(self.resource_group, self.name, param)
except Exception as exc:
self.fail('Error when creating automation account {0}: {1}'.format(self.name, exc.message))
def update_account_tags(self, param):
try:
return self.automation_client.automation_account.update(self.resource_group, self.name, param)
except Exception as exc:
self.fail('Error when updating automation account {0}: {1}'.format(self.name, exc.message))
def delete_account(self):
try:
return self.automation_client.automation_account.delete(self.resource_group, self.name)
except Exception as exc:
self.fail('Error when deleting automation account {0}: {1}'.format(self.name, exc.message))
def main():
AzureRMAutomationAccount()
if __name__ == '__main__':
main()
|