| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_automationaccount |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Automation account |
| 16 |
description: |
| 17 |
- Create, delete an Azure Automation account. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of resource group. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the automation account. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
state: |
| 30 |
description: |
| 31 |
- State of the automation account. Use C(present) to create or update a automation account and C(absent) to delete an automation account. |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
location: |
| 38 |
description: |
| 39 |
- Location of the resource. |
| 40 |
- If not specified, use resource group location. |
| 41 |
type: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- azure.azcollection.azure |
| 45 |
- azure.azcollection.azure_tags |
| 46 |
|
| 47 |
author: |
| 48 |
- Yuwei Zhou (@yuwzho) |
| 49 |
|
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = ''' |
| 53 |
- name: Create an automation account |
| 54 |
azure_rm_automationaccount: |
| 55 |
name: Testing |
| 56 |
resource_group: myResourceGroup |
| 57 |
|
| 58 |
- name: Create an automation account |
| 59 |
azure_rm_automationaccount: |
| 60 |
name: Testing |
| 61 |
resource_group: myResourceGroup |
| 62 |
location: eastus |
| 63 |
''' |
| 64 |
|
| 65 |
RETURN = ''' |
| 66 |
id: |
| 67 |
description: |
| 68 |
- Automation account resource ID. |
| 69 |
type: str |
| 70 |
returned: success |
| 71 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Automation/automationAccounts/Testing" |
| 72 |
''' |
| 73 |
|
| 74 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 75 |
|
| 76 |
try: |
| 77 |
from azure.core.exceptions import ResourceNotFoundError |
| 78 |
except ImportError: |
| 79 |
pass |
| 80 |
|
| 81 |
|
| 82 |
class AzureRMAutomationAccount(AzureRMModuleBase): |
| 83 |
|
| 84 |
def __init__(self): |
| 85 |
|
| 86 |
self.module_arg_spec = dict( |
| 87 |
resource_group=dict(type='str', required=True), |
| 88 |
name=dict(type='str', required=True), |
| 89 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 90 |
location=dict(type='str') |
| 91 |
) |
| 92 |
|
| 93 |
self.results = dict( |
| 94 |
changed=False, |
| 95 |
id=None |
| 96 |
) |
| 97 |
|
| 98 |
self.resource_group = None |
| 99 |
self.name = None |
| 100 |
self.state = None |
| 101 |
self.location = None |
| 102 |
|
| 103 |
super(AzureRMAutomationAccount, self).__init__(self.module_arg_spec, supports_check_mode=True) |
| 104 |
|
| 105 |
def exec_module(self, **kwargs): |
| 106 |
|
| 107 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 108 |
setattr(self, key, kwargs[key]) |
| 109 |
|
| 110 |
account = self.get_account() |
| 111 |
changed = False |
| 112 |
if self.state == 'present': |
| 113 |
if not account: |
| 114 |
if not self.location: |
| 115 |
resource_group = self.get_resource_group(self.resource_group) |
| 116 |
self.location = resource_group.location |
| 117 |
param = self.automation_models.AutomationAccountCreateOrUpdateParameters( |
| 118 |
location=self.location, |
| 119 |
sku=self.automation_models.Sku(name='Basic'), |
| 120 |
tags=self.tags |
| 121 |
) |
| 122 |
changed = True |
| 123 |
if not self.check_mode: |
| 124 |
account = self.create_or_update(param) |
| 125 |
elif self.tags: |
| 126 |
update_tags, tags = self.update_tags(account.tags) |
| 127 |
if update_tags: |
| 128 |
changed = True |
| 129 |
param = self.automation_models.AutomationAccountUpdateParameters( |
| 130 |
tags=tags |
| 131 |
) |
| 132 |
changed = True |
| 133 |
if not self.check_mode: |
| 134 |
self.update_account_tags(param) |
| 135 |
if account: |
| 136 |
self.results['id'] = account.id |
| 137 |
elif account: |
| 138 |
changed = True |
| 139 |
if not self.check_mode: |
| 140 |
self.delete_account() |
| 141 |
self.results['changed'] = changed |
| 142 |
return self.results |
| 143 |
|
| 144 |
def get_account(self): |
| 145 |
try: |
| 146 |
return self.automation_client.automation_account.get(self.resource_group, self.name) |
| 147 |
except ResourceNotFoundError: |
| 148 |
pass |
| 149 |
|
| 150 |
def create_or_update(self, param): |
| 151 |
try: |
| 152 |
return self.automation_client.automation_account.create_or_update(self.resource_group, self.name, param) |
| 153 |
except Exception as exc: |
| 154 |
self.fail('Error when creating automation account {0}: {1}'.format(self.name, exc.message)) |
| 155 |
|
| 156 |
def update_account_tags(self, param): |
| 157 |
try: |
| 158 |
return self.automation_client.automation_account.update(self.resource_group, self.name, param) |
| 159 |
except Exception as exc: |
| 160 |
self.fail('Error when updating automation account {0}: {1}'.format(self.name, exc.message)) |
| 161 |
|
| 162 |
def delete_account(self): |
| 163 |
try: |
| 164 |
return self.automation_client.automation_account.delete(self.resource_group, self.name) |
| 165 |
except Exception as exc: |
| 166 |
self.fail('Error when deleting automation account {0}: {1}'.format(self.name, exc.message)) |
| 167 |
|
| 168 |
|
| 169 |
def main(): |
| 170 |
AzureRMAutomationAccount() |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == '__main__': |
| 174 |
main() |