-
Notifications
You must be signed in to change notification settings - Fork 135
/
manage_job.py
116 lines (102 loc) · 3.22 KB
/
manage_job.py
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
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.automation import AutomationClient
from azure.mgmt.resource import ResourceManagementClient
def main():
SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None)
GROUP_NAME = "testgroupx"
JOB = "jobxxyyzz"
AUTOMATION_ACCOUNT = "automationaccountxxyyzz"
RUNBOOK = "Get-AzureVMTutorial"
# Create client
# # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
resource_client = ResourceManagementClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
automation_client = AutomationClient(
credential=DefaultAzureCredential(),
subscription_id=SUBSCRIPTION_ID
)
# Create resource group
resource_client.resource_groups.create_or_update(
GROUP_NAME,
{"location": "eastus"}
)
# - init depended resources -
# Create automation account
automation_account = automation_client.automation_account.create_or_update(
GROUP_NAME,
AUTOMATION_ACCOUNT,
{
"sku": {
"name": "Free"
},
"name": AUTOMATION_ACCOUNT,
"location": "East US 2"
}
)
print("Create automation account:\n{}".format(automation_account))
# Create runbook
runbook = automation_client.runbook.create_or_update(
GROUP_NAME,
AUTOMATION_ACCOUNT,
RUNBOOK,
{
"log_verbose": False,
"log_progress": True,
"runbook_type": "PowerShellWorkflow",
"publish_content_link": {
"uri": "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/0.0.0.3/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1",
"content_hash": {
"algorithm": "SHA256",
"value": "4fab357cab33adbe9af72ae4b1203e601e30e44de271616e376c08218fd10d1c"
},
},
"description": "Description of the Runbook",
"log_activity_trace": "1",
"name": RUNBOOK,
"location": "East US 2",
"tags": {
"tag01": "value01",
"tag02": "value02"
}
}
)
print("Create runbook:\n{}".format(runbook))
# - end -
# Create job
job = automation_client.job.create(
GROUP_NAME,
AUTOMATION_ACCOUNT,
JOB,
{
"runbook": {
"name": RUNBOOK
},
"parameters": {
"key01": "value01",
"key02": "value02"
},
"run_on": ""
}
)
print("Create job:\n{}".format(job))
# Get job
job = automation_client.job.get(
GROUP_NAME,
AUTOMATION_ACCOUNT,
JOB
)
print("Get job:\n{}".format(job))
# Delete Group
resource_client.resource_groups.begin_delete(
GROUP_NAME
).result()
if __name__ == "__main__":
main()