AzurePlaybook云计算自动化

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验

承接上一篇文章《微软Azure云 LAB 101:Azure PS发送RestAPI请求并将其作为自动化作业》的思路,如果换个角度使用Azure自动化(Azure automation)这个功能,效率是否会更好呢?

为此,作为一个初学者,我对Azure自动化(Azure automation)进行了初步研究学习。以下是使用“Azure automation schedule job”执行特定任务的实验和结果,希望可以帮助到大家打开新的玩法并了解更多Azure其他产品的知识。

[本文为AndyX.Net原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明!]

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图

 

[本文由AndyX撰写,本文的“Azure Portal”门户界面以英文为主,仅供参考。]

[本人中所有涉及到 Azure 的实验均在 Azure Global 国际版中进行配置,部分功能可能在 Azure 世纪互联中受到限制]

[本文为AndyX.Net原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明!]

 

实验目的:

承接上一篇文章《微软Azure云 LAB 101:Azure PS发送RestAPI请求并将其作为自动化作业》的思路,使用Azure自动化(Azure automation)功能实现类似的效果并降低计费成本。

 

关于计费:

说到“降低计费成本”那就不得不提及VM的计费和Azure automation计费的差异了。

Azure VM的计费:为“即用即付”,也就是说关于“计算类”的资源,仅开机时候开始计费。假设如果你需要实现每小时的自动化作业,那么按照之前的教程你需要保持一台VM始终处于开启状态。
以最基本较便宜的VM SKU:Standard B1ms为例,它拥有1个vCPU+2GB内存,基本上可以满足Windows操作系统的需求。以美国东部区域为例(不同区域计费不同,美东区较为便宜),该SKU“即用即付”的费用折合为0.0207$美刀/每小时=0.4968$美刀/每天=14.904$美刀/每月。

Azure 自动化的计费:进程自动化包括 Runbook 作业和观察程序。 作业的计费基于月份使用的作业运行分钟数,对于观察程序,它取决于一个月内使用的小时数。 作业或观察程序运行时,将产生进程自动化费用。 使用基本 SKU 创建 Azure 自动化帐户,其中每个订阅的前 500 个作业运行时分钟免费。 您只需为超过 500 分钟免费包含单位的分钟/小时付费。
也就是说如果按照每小时执行一次脚本,每次脚本执行时间大约为1分钟来计算,每个月执行时间理论上共计720分钟,其中前500分钟完全免费,只需要收取超过的220分钟部分。同样以部署在美国东部区域为例,自动化脚本执行的价格为0.002$美刀/每分钟,220分钟x0.002$=0.44$美刀/每月仅超出的计费

综上所述,使用Azure自动化(Azure automation)功能实现类似的效果会显著降低成本计费,如果将执行计划修改为每天执行,那么基本上等于完全免费了因为预计脚本执行时间不会超过500分钟每月。

 

先决条件:

  • 需要拥有对于“订阅(Subscription)”的“拥有者(Owner)”权限
  • 确保已经导入”Az.Accounts”, “Az.Security”模块到Azure自动化中(默认已导入)

*** 你可以在”Automation Accounts” -> “Shared Resources” -> “Modules”中检查模块导入状况 ***

 

详细步骤:

步骤一:创建自动化账户以及Runbook

1.按照正常步骤创建”自动化账户(Automation Accounts)”

2.导航至Runbooks列,然后点击”Create a runbook”

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图1

 

3.填写自定义名称,Runbook Type选择“Powershell”,Runtime版本选择“7.2”(旧版5.X亦可)

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图2

 

4.创建完成后,点击“编辑(Edit)”以便写入自定义脚本示例脚本(**注意更改你的SubID**):

#Set your SUBID here 改这里的xxx为你的SUBID别忘了
$subscriptionId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"

# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process


# Connect to Azure with system-assigned managed identity
Update-AzConfig -DefaultSubscriptionForLogin $subscriptionId
$AzureContext = (Connect-AzAccount -Identity).context

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext

$List = Get-AzSecurityPricing | Where-Object { $_.Name -ne "Api" } | Select-Object Name, PricingTier

foreach($item in $List)
{
    $name = $item.Name
    if ($item.PricingTier -ne 'Free')
    {
        try{
            $action = Set-AzSecurityPricing -Name $name -PricingTier "Free"
        }
        catch
        {
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
        Write-Output "Disabled defender for service $name"
    }
    else
    {
        Write-Output "no need change tier for $name"
    }
}

 

5.点击“保存(Save)”和“发布(Publish)”以便让脚本生效

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图3

 

步骤二:创建任务计划

1. 在”自动化账户(Automation Accounts)”中导航至“任务计划(Schedules)”列,然后点击”添加任务计划(Add a Schedule)”

2.设置为“已启用(Enabled)”,重复状态设置为”始终重复(Recurring)”,重复间隔设置在本次LAB中设置为”每小时执行(Recur every=1Hour)”,你可以根据实际情况自由配置为“每天”或者“每月”。

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图4

 

步骤三:将Runbook与任务计划(Schedules)关联

1.**重新回到**“Runbooks”页面,在该“Runbooks”页面下导航至“Schedules”,点击“Add a Schedule”

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图5

 

2.选择刚才已经创建的“任务计划(Schedules)”,保存即可完成“Runbooks”与“Schedules”的关联

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图6

 

步骤四:授权身份验证(Identity)

1.返回”自动化账户(Automation Accounts)”页面导航至“身份验证(Identity)”列,点击“角色分配(Azure Role Assignments)”

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图7

2.给该自动化账户的身份验证权限分配为:范围“Subscription”,订阅“你自己订阅的名字”,角色:“Contributor Role”。保存后生效。

微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验插图8

 

故障排查:

如果在调试中遇到类似的错误信息,请检查是否给”自动化账户(Automation Accounts)”的”身份验证(Identity)”授权了正确的“角色(Role)”.

Key          : DefaultSubscriptionForLogin
Value        : xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
Scope        : CurrentUser
AppliesTo    : Az
HelpMessage  : Subscription name or GUID. Sets the default context for Azure PowerShell when logging in without 
               specifying a subscription.
DefaultValue : 


The client '92144e27-xxxx-xxxx-xxxx-xxxxxxxxxxx' with object id '92144e27-xxxx-xxxx-xxxx-xxxxxxxxxxx' does not have authorization to perform action 'Microsoft.Resources/subscriptions/read' over scope '/subscriptions/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' or the scope is invalid. If access was recently granted, please refresh your credentials.

Connect-AzAccount: 
Line |
   8 |  $AzureContext = (Connect-AzAccount -Identity).context
     |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The provided account XXXXXX does not have access to subscription ID "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx". Please try logging in with different credentials or a different subscription ID. If a subscription is not specified, please check the configs by `Get-AzConfig`.

请按照上述“步骤四”授权“Subscription”范围的“Contributor Role”。

 

参考文献:

(全文完)

 

文章撰写:作者AndyX,来自AndyX.Net。技术指导Nina Li,来自Microsoft,特别感谢!

文章遵循 CC 4.0 BY-SA 版权协议,若需转载本文,请标注来源与链接:原创内容AndyX.Net版权所有

本文链接:微软Azure云 LAB 101: 使用Azure自动化作业定期执行特定任务的实验 – AndyX.Net – 安迪克斯

关联链接:微软Azure云 LAB 101:Azure PS发送RestAPI请求并将其作为自动化作业[中英双语] – AndyX.Net – 安迪克斯