You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial walks you through the creation of a [Python runbook](automation-runbook-types.md#python-runbooks) in Azure Automation. You start with a simple runbook that you test and publish. Then you modify the runbook to actually manage Azure resources, in this case starting an Azure virtual machine. Lastly, you make the runbook more robust by adding runbook parameters.
12
+
This tutorial walks you through the creation of a [Python runbook](../automation-runbook-types.md#python-runbooks) in Azure Automation. Python runbooks compile under Python 2. You can directly edit the code of the runbook using the text editor in the Azure portal.
13
+
14
+
> [!div class="checklist"]
15
+
> * Create a simple Python runbook
16
+
> * Test and publish the runbook
17
+
> * Run and track the status of the runbook job
18
+
> * Update the runbook to start an Azure virtual machine with runbook parameters
18
19
19
20
> [!NOTE]
20
21
> Using a webhook to start a Python runbook is not supported.
@@ -24,7 +25,7 @@ This tutorial walks you through the creation of a [Python runbook](automation-ru
24
25
To complete this tutorial, you need the following:
25
26
26
27
- Azure subscription. If you don't have one yet, you can [activate your MSDN subscriber benefits](https://azure.microsoft.com/pricing/member-offers/msdn-benefits-details/) or sign up for a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
27
-
-[Automation account](automation-offering-get-started.md) to hold the runbook and authenticate to Azure resources. This account must have permission to start and stop the virtual machine.
28
+
-[Automation account](../automation-offering-get-started.md) to hold the runbook and authenticate to Azure resources. This account must have permission to start and stop the virtual machine.
28
29
- An Azure virtual machine. You stop and start this machine so it should not be a production VM.
29
30
30
31
## Create a new runbook
@@ -33,13 +34,17 @@ You start by creating a simple runbook that outputs the text *Hello World*.
33
34
34
35
1. In the Azure portal, open your Automation account.
35
36
36
-
The Automation account page gives you a quick view of the resources in this account. You should already have some assets. Most of those assets are the modules that are automatically included in a new Automation account. You should also have the Credential asset that's mentioned in the [prerequisites](#prerequisites).<br>
37
+
The Automation account page gives you a quick view of the resources in this account. You should already have some assets. Most of those assets are the modules that are automatically included in a new Automation account. You should also have the Credential asset that's mentioned in the [prerequisites](#prerequisites).
38
+
39
+
2. Select **Runbooks** under **Process Automation** to open the list of runbooks.
40
+
41
+
3. Select **Add a runbook** to create a new runbook.
37
42
38
-
1. Select **Runbooks** under **Process Automation** to open the list of runbooks.
39
-
1. Select **Add a runbook** to create a new runbook.
40
-
1. Give the runbook the name **MyFirstRunbook-Python**.
41
-
1. Select **Python 2** for **Runbook type**.
42
-
1. Click **Create** to create the runbook and open the textual editor.
43
+
4. Give the runbook the name **MyFirstRunbook-Python**.
44
+
45
+
5. Select **Python 2** for **Runbook type**.
46
+
47
+
6. Click **Create** to create the runbook and open the textual editor.
43
48
44
49
## Add code to the runbook
45
50
@@ -56,11 +61,15 @@ Click **Save** to save the runbook.
56
61
Before you publish the runbook to make it available in production, you want to test it to make sure that it works properly. When you test a runbook, you run its Draft version and view its output interactively.
57
62
58
63
1. Click **Test pane** to open the Test pane.
59
-
1. Click **Start** to start the test. This should be the only enabled option.
60
-
1. A [runbook job](automation-runbook-execution.md) is created and its status displayed.
64
+
65
+
2. Click **Start** to start the test. This should be the only enabled option.
66
+
67
+
3. A [runbook job](../automation-runbook-execution.md) is created and its status displayed.
61
68
The job status starts as Queued, indicating that it is waiting for a runbook worker in the cloud to come available. It moves to Starting when a worker claims the job, and then Running when the runbook actually starts running.
62
-
1. When the runbook job completes, its output is displayed. In this case, you should see `Hello World`.
63
-
1. Close the Test pane to return to the canvas.
69
+
70
+
4. When the runbook job completes, its output is displayed. In this case, you should see `Hello World`.
71
+
72
+
5. Close the Test pane to return to the canvas.
64
73
65
74
## Publish and start the runbook
66
75
@@ -69,18 +78,30 @@ When you publish a runbook, you overwrite the existing published version with th
69
78
In this case, you don't have a published version yet because you just created the runbook.
70
79
71
80
1. Click **Publish** to publish the runbook and then **Yes** when prompted.
72
-
1. If you scroll left to view the runbook on the **Runbooks** page, you should see an **Authoring Status** of **Published**.
73
-
1. Scroll back to the right to view the pane for **MyFirstRunbook-Python**.
81
+
82
+
2. If you scroll left to view the runbook on the **Runbooks** page, you should see an **Authoring Status** of **Published**.
83
+
84
+
3. Scroll back to the right to view the pane for **MyFirstRunbook-Python**.
85
+
74
86
The options across the top allow you to start the runbook, view the runbook, or schedule it to start at some time in the future.
75
-
2. Click **Start** and then click **OK** when the Start Runbook blade opens.
76
-
3. A Job pane is opened for the runbook job that you created. You can close this pane, but let's leave it open so that you can watch the job's progress.
77
-
1. The job status is shown in **Job Summary** and matches the statuses that you saw when you tested the runbook.
78
-
2. Once the runbook status shows Completed, click **Output**. The Output pane is opened, where you can see `Hello World`.
79
-
3. Close the Output pane.
80
-
4. Click **All Logs** to open the Streams pane for the runbook job. You should only see `Hello World` in the Output stream. However, this pane can show other streams for a runbook job, such as Verbose and Error, if the runbook writes to them.
81
-
5. Close the Streams pane and the Job pane to return to the MyFirstRunbook-Python pane.
82
-
6. Click **Jobs** to open the Jobs page for this runbook. This page lists all jobs created by this runbook. You should only see one job listed since you only ran the job once.
83
-
7. You can click this job to open the same Job pane that you viewed when you started the runbook. This pane allows you to go back in time and view the details of any job that was created for a particular runbook.
87
+
88
+
4. Click **Start** and then click **OK** when the Start Runbook blade opens.
89
+
90
+
5. A Job pane is opened for the runbook job that you created. You can close this pane, but let's leave it open so that you can watch the job's progress.
91
+
92
+
6. The job status is shown in **Job Summary** and matches the statuses that you saw when you tested the runbook.
93
+
94
+
7. Once the runbook status shows Completed, click **Output**. The Output pane is opened, where you can see `Hello World`.
95
+
96
+
8. Close the Output pane.
97
+
98
+
9. Click **All Logs** to open the Streams pane for the runbook job. You should only see `Hello World` in the Output stream. However, this pane can show other streams for a runbook job, such as Verbose and Error, if the runbook writes to them.
99
+
100
+
10. Close the Streams pane and the Job pane to return to the MyFirstRunbook-Python pane.
101
+
102
+
11. Click **Jobs** to open the Jobs page for this runbook. This page lists all jobs created by this runbook. You should only see one job listed since you only ran the job once.
103
+
104
+
12. You can click this job to open the same Job pane that you viewed when you started the runbook. This pane allows you to go back in time and view the details of any job that was created for a particular runbook.
84
105
85
106
## Add authentication to manage Azure resources
86
107
@@ -162,11 +183,9 @@ Test and run the runbook again to see that it starts the VM.
162
183
163
184
## Use input parameters
164
185
165
-
The runbook currently uses hard-coded values for the names of the resource group and the VM.
166
-
Now let's add code that gets these values from input parameters.
186
+
The runbook currently uses hard-coded values for the names of the resource group and the VM. Now let's add code that gets these values from input parameters.
167
187
168
-
You use the `sys.argv` variable to get the parameter values.
169
-
Add the following code to the runbook immediately after the other `import` statements:
188
+
You use the `sys.argv` variable to get the parameter values. Add the following code to the runbook immediately after the other `import` statements:
This imports the `sys` module, and creates two variables to hold the Resource Group and VM names.
179
-
Notice that the element of the argument list, `sys.argv[0]`, is the name of the script, and is not input by the user.
197
+
This imports the `sys` module, and creates two variables to hold the Resource Group and VM names. Notice that the element of the argument list, `sys.argv[0]`, is the name of the script, and is not input by the user.
180
198
181
199
Now you can modify the last two lines of the runbook to use the input parameter values instead of using hard-coded values:
When you start a Python runbook (either on the Test pane or as a published runbook),
190
-
you can enter the values for parameters in the Start Runbook page under **Parameters**.
207
+
When you start a Python runbook (either on the Test pane or as a published runbook), you can enter the values for parameters in the Start Runbook page under **Parameters**.
191
208
192
-
After you start entering a value in the first box, a second appears, and so on,
193
-
so that you can enter as many parameter values as necessary.
209
+
After you start entering a value in the first box, a second appears, and so on, so that you can enter as many parameter values as necessary.
194
210
195
211
The values are available to the script in the `sys.argv` array as in the code you just added.
196
212
197
-
Enter the name of your resource group as the value for the first parameter,
198
-
and the name of the VM to start as the value of the second parameter.
213
+
Enter the name of your resource group as the value for the first parameter, and the name of the VM to start as the value of the second parameter.
0 commit comments