使用 Azure 资源管理器模板创建自动化帐户Create an Automation account by using an Azure Resource Manager template

可以使用 Azure 资源管理器模板在资源组中创建 Azure 自动化帐户。You can use Azure Resource Manager templates to create an Azure Automation account in your resource group. 本文提供了一个示例模板,该模板可以:This article provides a sample template that:

  • 自动创建 Azure Monitor Log Analytics 工作区。Automates the creation of an Azure Monitor Log Analytics workspace.
  • 自动创建 Azure 自动化帐户。Automates the creation of an Azure Automation account.
  • 将自动化帐户链接到 Log Analytics 工作区。Links the Automation account to the Log Analytics workspace.

该模板不会自动启用 Azure 或非 Azure 虚拟机。The template doesn't automate the enabling of Azure or non-Azure virtual machines.

Note

使用 Azure 资源管理器模板时,不支持创建自动化运行方式帐户。Creation of the Automation Run As account is not supported when you're using an Azure Resource Manager template. 若要从门户或使用 PowerShell 手动创建运行方式帐户,请参阅管理运行方式帐户To create a Run As account manually from the portal or with PowerShell, see Manage Run As accounts.

API 版本API versions

下表列出了此示例中使用的资源的 API 版本。The following table lists the API version for the resources used in this example.

资源Resource 资源类型Resource type API 版本API version
工作区Workspace workspacesworkspaces 2017-03-15-preview2017-03-15-preview
自动化帐户Automation account automationautomation 2015-10-312015-10-31

使用模板之前Before you use the template

如果选择在本地安装和使用 PowerShell,则本文需要 Azure PowerShell Az 模块。If you choose to install and use PowerShell locally, this article requires the Azure PowerShell Az module. 运行 Get-Module -ListAvailable Az 即可查找版本。Run Get-Module -ListAvailable Az to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install the Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。If you're running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure. 使用 PowerShell 时,此部署使用 New-AzResourceGroupDeploymentWith PowerShell, the deployment uses New-AzResourceGroupDeployment.

如果选择在本地安装并使用 Azure CLI,本文要求运行版本 2.1.0 或更高版本。If you choose to install and use the Azure CLI locally, this article requires that you're running version 2.1.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI. 使用 Azure CLI 时,此部署使用 az group deployment createWith the Azure CLI, this deployment uses az group deployment create.

配置 JSON 模板时,系统会提示你输入:The JSON template is configured to prompt you for:

  • 工作区的名称。The name of the workspace.
  • 要在其中创建工作区的区域。The region to create the workspace in.
  • 自动化帐户的名称。The name of the Automation account.
  • 要在其中创建帐户的区域。The region to create the account in.

使用 Log Analytics 工作区的默认值设置模板中的以下参数:The following parameters in the template are set with a default value for the Log Analytics workspace:

  • sku 默认为“按 GB”定价层,该层已在 2018 年 4 月的定价模型中发布。sku defaults to the per GB pricing tier released in the April 2018 pricing model.
  • dataRetention 默认为 30 天。dataRetention defaults to 30 days.
  • capacityReservationLevel 默认为 100 GB。capacityReservationLevel defaults to 100 GB.

Warning

如果要在订阅中创建或配置 Log Analytics 工作区,而该订阅已加入 2018 年 4 月的定价模型,则唯一有效的 Log Analytics 定价层为 PerGB2018。If you want to create or configure a Log Analytics workspace in a subscription that has opted into the April 2018 pricing model, the only valid Log Analytics pricing tier is PerGB2018.

JSON 模板为其他参数指定默认值,这些参数将会用作环境中的标准配置。The JSON template specifies a default value for the other parameters that would likely be used as a standard configuration in your environment. 可以将模板存储在 Azure 存储帐户中,以便在组织中共享访问。You can store the template in an Azure storage account for shared access in your organization. 有关使用模板的更多信息,请参阅使用资源管理器模板和 Azure CLI 部署资源For more information about working with templates, see Deploy resources with Resource Manager templates and the Azure CLI.

如果你不熟悉 Azure 自动化和 Azure Monitor,请务必了解以下配置详细信息。If you're new to Azure Automation and Azure Monitor, it's important that you understand the following configuration details. 当你尝试创建、配置和使用链接到新自动化帐户的 Log Analytics 工作区时,这些信息有助于你避免出现错误。They can help you avoid errors when you try to create, configure, and use a Log Analytics workspace linked to your new Automation account.

  • 查看更多详细信息以充分了解工作区配置选项,如访问控制模式、定价层、保留期和产能预留级别。Review additional details to fully understand workspace configuration options, such as access control mode, pricing tier, retention, and capacity reservation level.

  • 如果你不熟悉 Azure Monitor 日志,并且尚未部署工作区,则应查看工作区设计指南If you're new to Azure Monitor logs and have not deployed a workspace already, you should review the workspace design guidance. 该指南有助于你了解访问控制,并了解我们为你的组织推荐的设计实现策略。It will help you to learn about access control, and understand the design implementation strategies we recommend for your organization.

部署模板Deploy the template

  1. 将以下 JSON 语法复制并粘贴到该文件中:Copy and paste the following JSON syntax into your file:

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "workspaceName": {
            "type": "string",
            "metadata": {
                "description": "Workspace name"
            }
        },
        "sku": {
            "type": "string",
            "allowedValues": [
                "pergb2018",
                "Free",
                "Standalone",
                "PerNode",
                "Standard",
                "Premium"
            ],
            "defaultValue": "pergb2018",
            "metadata": {
                "description": "Pricing tier: perGB2018 or legacy tiers (Free, Standalone, PerNode, Standard or Premium), which are not available to all customers."
            }
        },
        "dataRetention": {
            "type": "int",
            "defaultValue": 30,
            "minValue": 7,
            "maxValue": 730,
            "metadata": {
                "description": "Number of days of retention. Workspaces in the legacy Free pricing tier can have only 7 days."
            }
        },
        "immediatePurgeDataOn30Days": {
            "type": "bool",
            "defaultValue": "[bool('false')]",
            "metadata": {
                "description": "If set to true when changing retention to 30 days, older data will be immediately deleted. Use this with extreme caution. This applies only when retention is being set to 30 days."
            }
        },
        "location": {
            "type": "string",
            "metadata": {
                "description": "Specifies the location in which to create the workspace."
            }
        },
        "automationAccountName": {
            "type": "string",
            "metadata": {
                "description": "Automation account name"
            }
        },
        "automationAccountLocation": {
            "type": "string",
            "metadata": {
                "description": "Specify the location in which to create the Automation account."
            }
        },
        "sampleGraphicalRunbookName": {
                "type": "String",
                "defaultValue": "AzureAutomationTutorial"
            },
            "sampleGraphicalRunbookDescription": {
                "type": "String",
                "defaultValue": " An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
            },
            "sampleGraphicalRunbookContentUri": {
                "type": "String",
                "defaultValue": "https://eus2oaasibizamarketprod1.blob.core.windows.net/marketplace-runbooks/AzureAutomationTutorial.graphrunbook"
            },
            "samplePowerShellRunbookName": {
                "type": "String",
                "defaultValue": "AzureAutomationTutorialScript"
            },
            "samplePowerShellRunbookDescription": {
                "type": "String",
                "defaultValue": " An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
            },
            "samplePowerShellRunbookContentUri": {
                "type": "String",
                "defaultValue": "https://eus2oaasibizamarketprod1.blob.core.windows.net/marketplace-runbooks/AzureAutomationTutorial.ps1"
            },
            "samplePython2RunbookName": {
                "type": "String",
                "defaultValue": "AzureAutomationTutorialPython2"
            },
            "samplePython2RunbookDescription": {
                "type": "String",
                "defaultValue": " An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
            },
            "samplePython2RunbookContentUri": {
                "type": "String",
                "defaultValue": "https://eus2oaasibizamarketprod1.blob.core.windows.net/marketplace-runbooks/AzureAutomationTutorialPython2.py"
            }
    },
    "resources": [
        {
        "type": "Microsoft.OperationalInsights/workspaces",
            "name": "[parameters('workspaceName')]",
            "apiVersion": "2017-03-15-preview",
            "location": "[parameters('location')]",
            "properties": {
                "sku": {
                    "Name": "[parameters('sku')]",
                    "name": "CapacityReservation",
                    "capacityReservationLevel": 100
                },
                "retentionInDays": "[parameters('dataRetention')]",
                "features": {
                    "searchVersion": 1,
                    "legacy": 0,
                    "enableLogAccessUsingOnlyResourcePermissions": true
                }
            },
        "resources": [
        {
            "type": "Microsoft.Automation/automationAccounts",
            "apiVersion": "2015-01-01-preview",
            "name": "[parameters('automationAccountName')]",
            "location": "[parameters('automationAccountLocation')]",
            "dependsOn": [
                 "[concat('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]"
            ],
            "tags": {},
            "properties": {
                "sku": {
                    "name": "Basic"
                }
            },
            "resources": [
                    {
                        "type": "runbooks",
                        "apiVersion": "2015-01-01-preview",
                        "name": "[parameters('sampleGraphicalRunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "dependsOn": [
                            "[concat('Microsoft.Automation/automationAccounts/', parameters('automationAccountName'))]"
                        ],
                        "tags": {},
                        "properties": {
                            "runbookType": "GraphPowerShell",
                            "logProgress": "false",
                            "logVerbose": "false",
                            "description": "[parameters('sampleGraphicalRunbookDescription')]",
                            "publishContentLink": {
                                "uri": "[parameters('sampleGraphicalRunbookContentUri')]",
                                "version": "1.0.0.0"
                            }
                        }
                    },
                    {
                        "type": "runbooks",
                        "apiVersion": "2015-01-01-preview",
                        "name": "[parameters('samplePowerShellRunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "dependsOn": [
                            "[concat('Microsoft.Automation/automationAccounts/', parameters('automationAccountName'))]"
                        ],
                        "tags": {},
                        "properties": {
                            "runbookType": "PowerShell",
                            "logProgress": "false",
                            "logVerbose": "false",
                            "description": "[parameters('samplePowerShellRunbookDescription')]",
                            "publishContentLink": {
                                "uri": "[parameters('samplePowerShellRunbookContentUri')]",
                                "version": "1.0.0.0"
                            }
                        }
                    },
                    {
                        "type": "runbooks",
                        "apiVersion": "2015-01-01-preview",
                        "name": "[parameters('samplePython2RunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "dependsOn": [
                            "[concat('Microsoft.Automation/automationAccounts/', parameters('automationAccountName'))]"
                        ],
                        "tags": {},
                        "properties": {
                            "runbookType": "Python2",
                            "logProgress": "false",
                            "logVerbose": "false",
                            "description": "[parameters('samplePython2RunbookDescription')]",
                            "publishContentLink": {
                                "uri": "[parameters('samplePython2RunbookContentUri')]",
                                "version": "1.0.0.0"
                            }
                        }
                    }
                ]
        },
        {
            "apiVersion": "2015-11-01-preview",
            "type": "Microsoft.OperationalInsights/workspaces/linkedServices",
            "name": "[concat(parameters('workspaceName'), '/' , 'Automation')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[concat('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]",
                "[concat('Microsoft.Automation/automationAccounts/', parameters('automationAccountName'))]"
            ],
            "properties": {
                "resourceId": "[resourceId('Microsoft.Automation/automationAccounts/', parameters('automationAccountName'))]"
            }
        }
       ]
      }
     ]
    }
    
  2. 按要求编辑模板。Edit the template to meet your requirements. 请考虑创建资源管理器参数文件,而不是将参数作为内联值传递。Consider creating a Resource Manager parameter file instead of passing parameters as inline values.

  3. 在本地文件夹中将此文件另存为 deployAzAutomationAccttemplate.json。Save this file as deployAzAutomationAccttemplate.json to a local folder.

  4. 已做好部署此模板的准备。You're ready to deploy this template. 可以使用 PowerShell 或 Azure CLI。You can use either PowerShell or the Azure CLI. 当系统提示你输入工作区和自动化帐户名称时,请提供在所有 Azure 订阅中全局唯一的名称。When you're prompted for a workspace and Automation account name, provide a name that's globally unique across all your Azure subscriptions.

    PowerShellPowerShell

    New-AzResourceGroupDeployment -Name <deployment-name> -ResourceGroupName <resource-group-name> -TemplateFile deployAzAutomationAccttemplate.json
    

    Azure CLIAzure CLI

    az group deployment create --resource-group <my-resource-group> --name <my-deployment-name> --template-file deployAzAutomationAccttemplate.json
    

    部署可能需要几分钟才能完成。The deployment can take a few minutes to finish. 完成后,你会看到一条如下所示的消息,其中包含结果。When it does, you'll see a message like the following that includes the result.

    部署完成后的示例结果

后续步骤Next steps

有了自动化帐户之后,接下来可以创建 Runbook 并自动执行手动流程。Now that you have an Automation account, you can create runbooks and automate manual processes.