Automate cloud operations with Azure automation (4)

Source: Internet
Author: User

After the basic preparations have been done, we'll look at how to implement a timed automatic switch using Azure automation, which is ideal for the dev/test environment, because Azure virtual machines are billed in minutes, So we can save costs by turning on the virtual machine while the Dev tester is working, and turning off the virtual machine at the end of the week, which is also a cloud-based flexibility on a different level.

Let's start with the basic principles of design and how you can make your automation scripts more flexible and portable:

    • Parameterization: Try not to write any variables in the script, except to modify the code, there is no other way, and maintainability will become very poor
    • Rational use of assets: in the Azure automation, there are some configurations that need to be updated and changed, so this part of the configuration should be in the asset, let the OPS staff to modify, such as which virtual machines need to boot, shutdown
    • Modularity: Some common scripts can be written as runbooks for code reuse and sharing
DEMO: Create a runbook that shuts down VMS by a specified list
    1. 1. Login to the portal, select New Runbook, select Create from library, you can see that there is actually a lot of built-in scripts, we choose featured inside the Stop Azure Classic VMs, the next step, you can see the definition of the script:

      ?

    2. 2. Enter the name of the runbook you want, select the automation account that we created, select the region, and choose Finish to add the Runbook to your Automation account:

      ?

    3. 3. Open the Automation account, in the Runbook tab you can just create the Runbook stop-azurevmlib, click on this Runbook, select authoring, you can see the full source of this script, and can be modified, at the bottom

      ?

      ?

    4. 4. The basic structure of the runbook is as follows, starting with workflow, followed by your runbook name, param is your input parameter, OutputType inside defines a string type of return result:

      ?

      ?

      ?

      Workflow Stop-azurevmlib

      {

      Param (

      [Parameter (mandatory= $false)]

      [String] $ServiceName

      )

      ?

      # Returns strings with status messages

      [OutputType ([String])]

      ?

      Body

      }

      ?

      5. Now we are designing a STOP-VM that can be used in real-world situations to achieve the following goals:

    • Users can configure which VMS need to be stopped at regular intervals without having to modify the code
    • Authentication information can be modified without the need to modify the code
    • Saturday no scheduled shutdown or boot on Sundays
    • Detailed Execution log information

      ?

6. Use Azure username password for authentication, related orgid, password we are all kept in the asset, through dynamic access, without the need to modify the code, through get-automationvariable The resulting data can be added to the added asset to get the variable:

#资产中定义的用户名和订阅名

$AzureCredentialAssetName = ' [email protected] '

$AzureSubscriptionIdAssetName = ' Automationsubid '

?

# Get the user name password and subscription name defined in the asset

$Cred = Get-automationpscredential-name $AzureCredentialAssetName

$SubId = Get-automationvariable-name $AzureSubscriptionIdAssetName

?

#获得需要关闭的虚拟机列表, separated by commas

$vmconfiglist = Get-automationvariable-name ' vmnamelist '

?

$vmlist = $vmconfiglist-split ","

?

#获得认证, add a subscription for subsequent operations

$null = add-azureaccount-credential $Cred-environment azurechinacloud-erroraction Stop

?????

$null = Select-azuresubscription-subscriptionid $SubId-erroraction Stop

?

7. How do I get certified if I use a certificate?

???? $AzureSubscriptionIdAssetName = ' automationsubid '

$subscriptionNameAssetname = ' Azuresubscriptionname '

# get a subscription ID and subscription name

$SubId = Get-automationvariable-name $AzureSubscriptionIdAssetName

$subscriptionName = Get-automationvariable-name $subscriptionNameAssetname

#获得认证名称和证书

$certificateName = Get-automationvariable-name "Mycertificatename"

$certificate = Get-automationcertificate-name $certificateName

#根据证书这是当前订阅

Set-azuresubscription-subscriptionname $subscriptionName-subscriptionid $SubId-certificate $certificate- Environment Azurechinacloud-erroraction Stop

Select-azuresubscription-subscriptionid $SubId-erroraction Stop

8. The rest is based on the virtual machine machine name to get the virtual machine to stop the operation of the virtual machine, all the source code I put on GitHub, please download the test separately:

To stop a virtual machine by means of a user name password authentication:

Https://github.com/kingliantop/azurelabs/blob/master/automation/stop-azurevms.ps1

To start the virtual machine by using the user name Password authentication method:

Https://github.com/kingliantop/azurelabs/blob/master/automation/start-azurevms.ps1

To stop a virtual machine by way of a certificate:

Https://github.com/kingliantop/azurelabs/blob/master/automation/stop-azurevmswithcert.ps1

To start a virtual machine by means of a certificate:

Https://github.com/kingliantop/azurelabs/blob/master/automation/start-azurevmswithcert.ps1

9. Runbook Editing is complete, click Publish to publish the Runbook, but this time the scripts do not run automatically, you need to set a timed run, select the schedule in the Runbook, define when to run the scripts, choose to link to the new plan:

10. Enter the name of the plan, set the schedule of the scheduled shutdown every day, you can see the current setup options to the hour level, or every day, but no weekday or weekend definition, you need to write code implementation, specific implementation please refer to the code.

?

11. Each runbook needs to set up the schedule, in this way, you can realize the morning boot, night shutdown, the weekend does not switch machine operations.

Automating cloud Operations with Azure automation (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.