This is the 2nd part of a 3 part Blog Series on Mastering Azure Automation.
|1||Mastering Azure Automation (Part 1 of 3 Learning ARM Templates)|
|2||Mastering Azure Automation (Part 2 of 3 Learning Azure runbooks)|
|3||Mastering Azure Automation (Part 3 of 3 Learning Desired State configurations)|
What is a Runbook?
Runbooks deliver the core functionality of Azure Automation by serving as containers for your custom scripts and workflows. In addition, runbooks typically reference Automation assets, such as credentials, variables, connections, and certificates. They also can contain other runbooks, thereby allowing you to build more complex workflows. You can invoke and run runbooks either on demand or according to your chosen schedule by leveraging Automation Schedule assets.
You can create your own runbooks from scratch or modify runbooks from the Runbook Gallery for your own requirements. There are three different runbook types that you can choose from based on your requirements and PowerShell experience. If you prefer to work directly with the PowerShell code, then you can use a PowerShell runbook or a PowerShell Workflow runbook that you can edit offline or with the textual editor in the Azure portal. If you prefer to edit a runbook without being exposed to the underlying code, then you can create a Graphical runbook using the graphical editor in the Azure portal.
Azure have two types of runbooks
- Graphical Runbooks
- Powershell Runbooks
Graphical runbooks allow you to create runbooks for Azure Automation without the complexities of the underlying Windows PowerShell or PowerShell Workflow code. You create your workflow by adding activities from the library, linking them together, and then configuring the components.
Graphical and Graphical PowerShell Workflow runbooks are created and edited with the graphical editor in the Azure portal. You can export them to a file and then import them into another automation account, but you cannot create or edit them with another tool. Graphical runbooks generate PowerShell code, but you can’t directly view or modify the code. Graphical runbooks cannot be converted to one of the text formats nor can a text runbook be converted to graphical format. Graphical runbooks can be converted to Graphical PowerShell Workflow runbooks during import and vice-versa.
The textual editor in Azure Automation can be used to edit PowerShell runbooks and PowerShell Workflow runbooks. The code editor has intellisense and color coding with additional special features to assist you in accessing resources common to runbooks.
You can insert code for activities, assets, and child runbooks into a runbook. Rather than typing in the code yourself, you can select from a list of available resources and have the appropriate code inserted into the runbook.
Each runbook in Azure Automation has two versions, Draft and Published. You edit the Draft version of the runbook and then Publish it so it can be executed. The Published version cannot be edited.
Lets create a simple graphical runbook.
Step 1: Create a Azure Automation Account through Azure Portal.
Step 2: Now opening up Azure Automation Account that I just created and under Process Automation category I clicked on RunBooks and then click “Add a runbook“.
Step 3: Now create Graphical runbook and edit it.
Step 4: In the Library control, click in the search textbox and type Write-Output. The search results are shown in the following image:
Step 5: Click the Write-Output activity on the canvas. This action opens the Configuration control page, which allows you to configure the activity.
- The Label defaults to the name of the cmdlet, but you can change it to something more friendly. Change it to Write Hello World to output.
- Click Parameters to provide values for the cmdlet’s parameters.
- Some cmdlets have multiple parameter sets, and you need to select which you one to use. In this case, Write-Output has only one parameter set, so you don’t need to select one.
- Select the InputObject parameter. This is the parameter where you specify the text to send to the output stream.
- In the Data source dropdown, select PowerShell expression. The Data source dropdown provides different sources that you use to populate a parameter value.
- You can use output from such sources such as another activity, an Automation asset, or a PowerShell expression. In this case, the output is just Hello World. You can use a PowerShell expression and specify a string.
- In the Expression box, type “Hello from Cloud & Mobile Blog” and then click OK twice to return to the canvas.
- Save the runbook by clicking Save.
Step 6: 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.
- Select Test pane to open the Test page.
- Click Start to start the test. This should be the only enabled option.
- A runbook job is created and its status displayed in the pane.
- When the runbook job completes, its output is displayed. In this case, you see Hello from Cloud & Mobile Blog” as shown below
Publish and start the runbook
The runbook that you created is still in Draft mode. It needs to be published before you can run it in production. When you publish a runbook, you overwrite the existing Published version with the Draft version. In this case, you don’t have a Published version yet because you just created the runbook.
- Select Publish to publish the runbook and then Yes when prompted.
- If you scroll left to view the runbook in the Runbooks page, it shows an Authoring Status of Published.
- Scroll back to the right to view the page for MyFirstGraphicalRunbookThe options across the top allow us to start the runbook, schedule it to start at some time in the future, or create a webhook so it can be started through an HTTP call.
- Select Start and then Yes when prompted to start the runbook.
- A job page is opened for the runbook job that was created. Verify that the Job status shows Completed.
- Once the runbook status shows Completed, click Output. The Output page is opened, and you can see the “Hello from Cloud and Mobile Blog” in the pane as shown below.
- Close the Output page.
- Click All Logs to open the Streams page for the runbook job. You should only see Hello World in the output stream, but this can show other streams for a runbook job such as Verbose and Error if the runbook writes to them.
- Close the All Logs page and the Job page to return to the MyFirstGraphicalRunbook page.
- To view all the jobs for the runbook close the Job page and select Jobs under RESOURCES. This lists all the jobs created by this runbook. You should only see one job listed since you only ran the job once.
- You can click this job to open the same Job pane that you viewed when you started the runbook. This allows you to go back in time and view the details of any job that was created for a particular runbook.