在 Azure 中创建管理解决方案文件(预览版)Creating a management solution file in Azure (Preview)

备注

这是在 Azure 中创建管理解决方案(当前处于预览状态)的初步文档。This is preliminary documentation for creating management solutions in Azure which are currently in preview. 如下所述的全部架构均会有变动。Any schema described below is subject to change.

Azure 中的管理解决方案作为资源管理器模板实施。Management solutions in Azure are implemented as Resource Manager templates. 了解如何创作管理解决方案的主要任务是了解如何创作模板The main task in learning how to author management solutions is learning how to author a template. 本文提供了用于解决方案的模板以及如何配置典型的解决方案资源的唯一详细信息。This article provides unique details of templates used for solutions and how to configure typical solution resources.

工具Tools

可使用任何文本编辑器处理解决方案文件,但我们建议利用 Visual Studio 或 Visual Studio Code 提供的功能,如以下文章中所述。You can use any text editor to work with solution files, but we recommend leveraging the features provided in Visual Studio or Visual Studio Code as described in the following articles.

结构Structure

管理解决方案文件的基本结构与如下所示的资源管理器模板相同。The basic structure of a management solution file is the same as a Resource Manager Template, which is as follows. 以下各节描述了解决方案中的顶级元素及其内容。Each of the sections below describes the top-level elements and their contents in a solution.

{
   "$schema": "https://schema.management.chinacloudapi.cn/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion": "1.0",
   "parameters": {  },
   "variables": {  },
   "resources": [  ],
   "outputs": {  }
}

parametersParameters

parameters 是你在用户安装管理解决方案时从用户请求的值。Parameters are values that you require from the user when they install the management solution. 存在所有解决方案均具有的标准参数,你也可以根据特定解决方案的需要添加其他参数。There are standard parameters that all solutions will have, and you can add additional parameters as required for your particular solution. 用户安装解决方案时提供参数值的方式取决于特定参数和解决方案安装方式。How users will provide parameter values when they install your solution will depend on the particular parameter and how the solution is being installed.

用户通过 Azure 市场或 Azure 快速入门模板安装管理解决方案时,系统会提示他们选择 Log Analytics 工作区和自动化帐户When a user installs your management solution through the Azure Marketplace or Azure QuickStart templates they are prompted to select a Log Analytics workspace and Automation account. 这些用于填充每个标准参数的值。These are used to populate the values of each of the standard parameters. 系统不提示用户直接提供标准参数的值,但会提示他们提供任何其他参数的值。The user is not prompted to directly provide values for the standard parameters, but they are prompted to provide values for any additional parameters.

下面显示了一个示例参数。A sample parameter is shown below.

"startTime": {
    "type": "string",
    "metadata": {
        "description": "Enter time for starting VMs by resource group.",
        "control": "datetime",
        "category": "Schedule"
    }

下表描述了参数属性。The following table describes the attributes of a parameter.

属性Attribute 说明Description
typetype 参数的数据类型。Data type for the parameter. 向用户显示的输入控件取决于数据类型。The input control displayed for the user depends on the data type.

bool - 下拉框bool - Drop down box
string - 文本框string - Text box
int - 文本框int - Text box
securestring - 密码字段securestring - Password field
categorycategory 参数的可选类别。Optional category for the parameter. 相同类别中的参数分到一组。Parameters in the same category are grouped together.
控制control 字符串参数的其他功能。Additional functionality for string parameters.

datetime - 显示 Datetime 控件。datetime - Datetime control is displayed.
guid - 自动生成的 Guid 值,不显示参数。guid - Guid value is automatically generated, and the parameter is not displayed.
descriptiondescription 参数的可选说明。Optional description for the parameter. 显示在参数旁边信息气球。Displayed in an information balloon next to the parameter.

标准参数Standard parameters

下表列出了所有管理解决方案的标准参数。The following table lists the standard parameters for all management solutions. 从 Azure 市场或快速入门模板安装解决方案时,这些值用于为用户进行填充而不是提示他们。These values are populated for the user instead of prompting for them when your solution is installed from the Azure Marketplace or Quickstart templates. 如果使用其他方法安装解决方案,则用户必须为其提供值。The user must provide values for them if the solution is installed with another method.

备注

Azure 市场和快速入门模板中的用户界面正在等待表中的参数名称。The user interface in the Azure Marketplace and Quickstart templates is expecting the parameter names in the table. 如果使用不同的参数名称,则将对用户进行提示,这些参数不会自动填充。If you use different parameter names then the user will be prompted for them, and they will not be automatically populated.

参数Parameter 类型Type 说明Description
accountNameaccountName stringstring Azure 自动化帐户名称。Azure Automation account name.
pricingTierpricingTier stringstring Log Analytics 工作区和 Azure 自动化帐户的定价层。Pricing tier of both Log Analytics workspace and Azure Automation account.
regionIdregionId stringstring Azure 自动化帐户的区域。Region of the Azure Automation account.
solutionNamesolutionName stringstring 解决方案名称。Name of the solution. 如果要通过快速入门模板部署解决方案,则应将 solutionName 定义为参数,以便能够定义字符串,而无需用户指定一个字符串。If you are deploying your solution through Quickstart templates, then you should define solutionName as a parameter so you can define a string instead requiring the user to specify one.
workspaceNameworkspaceName stringstring Log Analytics 工作区名称。Log Analytics workspace name.
workspaceRegionIdworkspaceRegionId stringstring Log Analytics 工作区的区域。Region of the Log Analytics workspace.

以下是可以复制并粘贴到解决方案文件的标准参数的结构。Following is the structure of the standard parameters that you can copy and paste into your solution file.

"parameters": {
    "workspaceName": {
        "type": "string",
        "metadata": {
            "description": "A valid Log Analytics workspace name"
        }
    },
    "accountName": {
           "type": "string",
           "metadata": {
               "description": "A valid Azure Automation account name"
           }
    },
    "workspaceRegionId": {
           "type": "string",
           "metadata": {
               "description": "Region of the Log Analytics workspace"
        }
    },
    "regionId": {
        "type": "string",
        "metadata": {
            "description": "Region of the Azure Automation account"
        }
    },
    "pricingTier": {
        "type": "string",
        "metadata": {
            "description": "Pricing tier of both Log Analytics workspace and Azure Automation account"
        }
    }
}

请参阅解决方案的其他元素中语法为 parameters('parameter name') 的参数值。You refer to parameter values in other elements of the solution with the syntax parameters('parameter name'). 例如,要访问工作区名称,将使用 parameters('workspaceName')For example, to access the workspace name, you would use parameters('workspaceName')

变量Variables

Variables 是会在管理解决方案的其余部分使用的值。Variables are values that you will use in the rest of the management solution. 这些值不公开给安装解决方案的用户。These values are not exposed to the user installing the solution. 它们旨在向作者提供一个位置,供作者在该位置中管理可在整个解决方案中使用多次的值。They are intended to provide the author with a single location where they can manage values that may be used multiple times throughout the solution. 应将任何特定于解决方案的值放在变量中,而不是在 resources 元素中对其进行硬编码。You should put any values specific to your solution in variables as opposed to hard coding them in the resources element. 这使代码更具可读性,并允许在更高版本中轻松地更改这些值。This makes the code more readable and allows you to easily change these values in later versions.

下面是具有在解决方案中使用的典型参数的 variables 元素示例。Following is an example of a variables element with typical parameters used in solutions.

"variables": {
    "SolutionVersion": "1.1",
    "SolutionPublisher": "Contoso",
    "SolutionName": "My Solution",
    "LogAnalyticsApiVersion": "2015-11-01-preview",
    "AutomationApiVersion": "2015-10-31"
},

引用整个解决方案中的变量值时的语法为 variables('variable name') 的变量值。You refer to variable values through the solution with the syntax variables('variable name'). 例如,若要访问 SolutionName 变量,需使用 variables('SolutionName')For example, to access the SolutionName variable, you would use variables('SolutionName').

还可定义包含多组值的复杂变量。You can also define complex variables that multiple sets of values. 这些复杂变量对以下管理解决方案特别有用:为不同类型的资源定义多个属性。These are particularly useful in management solutions where you are defining multiple properties for different types of resources. 例如,可将如上所示的解决方案变量重构到以下内容。For example, you could restructure the solution variables shown above to the following.

"variables": {
    "Solution": {
      "Version": "1.1",
      "Publisher": "Contoso",
      "Name": "My Solution"
    },
    "LogAnalyticsApiVersion": "2015-11-01-preview",
    "AutomationApiVersion": "2015-10-31"
},

在此情况下,引用整个解决方案中语法为 variables('variable name').property 的变量值。In this case, you refer to variable values through the solution with the syntax variables('variable name').property. 例如,若要访问 Solution Name 变量,需使用 variables('Solution').NameFor example, to access the Solution Name variable, you would use variables('Solution').Name.

资源Resources

Resources 用于定义管理解决方案要安装和配置的不同资源。Resources define the different resources that your management solution will install and configure. 这会是模板的最大且最复杂的部分。This will be the largest and most complex portion of the template. 可在创作 Azure 资源管理器模板中获取资源元素的结构和完整说明。You can get the structure and complete description of resource elements in Authoring Azure Resource Manager templates. 本文档的其他文章中详述了通常需定义的其他资源。Different resources that you will typically define are detailed in other articles in this documentation.

依赖项Dependencies

dependsOn 元素指定对另一个资源的依赖The dependsOn element specifies a dependency on another resource. 安装解决方案时,资源的所有依赖均已创建后才能创建资源。When the solution is installed, a resource is not created until all of its dependencies have been created. 例如,如果解决方案使用作业资源安装,则该解决方案需要启动 runbookFor example, your solution might start a runbook when it's installed using a job resource. 作业资源将依赖于 runbook 资源,以确保在创建作业之前创建 runbook。The job resource would be dependent on the runbook resource to make sure that the runbook is created before the job is created.

Log Analytics 工作区和自动化帐户Log Analytics workspace and Automation account

管理解决方案需要 Log Analytics 工作区来包含视图,也需要自动化帐户来包含 runbook 和相关资源。Management solutions require a Log Analytics workspace to contain views and an Automation account to contain runbooks and related resources. 这些内容在解决方案中的资源创建之前必须已经存在,并且不能在解决方案本身中定义。These must be available before the resources in the solution are created and should not be defined in the solution itself. 部署解决方案时,用户将指定工作区和帐户,但作为作者,应考虑以下几点。The user will specify a workspace and account when they deploy your solution, but as the author you should consider the following points.

解决方案资源Solution resource

每个解决方案在 resources 元素中都需要资源项,用于定义解决方案本身。Each solution requires a resource entry in the resources element that defines the solution itself. 该资源项的类型为 Microsoft.OperationsManagement/solutions,并且具有以下结构。This will have a type of Microsoft.OperationsManagement/solutions and have the following structure. 这包括常用于定义解决方案属性的标准参数变量This includes standard parameters and variables that are typically used to define properties of the solution.

{
  "name": "[concat(variables('Solution').Name, '[' ,parameters('workspaceName'), ']')]",
  "location": "[parameters('workspaceRegionId')]",
  "tags": { },
  "type": "Microsoft.OperationsManagement/solutions",
  "apiVersion": "[variables('LogAnalyticsApiVersion')]",
  "dependsOn": [
    <list-of-resources>
  ],
  "properties": {
    "workspaceResourceId": "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceName'))]",
    "referencedResources": [
        <list-of-referenced-resources>
    ],
    "containedResources": [
        <list-of-contained-resources>
    ]
  },
  "plan": {
    "name": "[concat(variables('Solution').Name, '[' ,parameters('workspaceName'), ']')]",
    "Version": "[variables('Solution').Version]",
    "product": "[variables('ProductName')]",
    "publisher": "[variables('Solution').Publisher]",
    "promotionCode": ""
  }
}

依赖项Dependencies

解决方案资源必须具有针对解决方案中所有其他资源的依赖,因为它们在解决方案创建之前必须已经存在。The solution resource must have a dependency on every other resource in the solution since they need to exist before the solution can be created. 通过在 dependsOn 元素中添加每个资源的项实现此目的。You do this by adding an entry for each resource in the dependsOn element.

属性Properties

解决方案资源具有下表中的属性。The solution resource has the properties in the following table. 这包括由用于定义安装解决方案后如何管理资源的解决方案引用和包含的资源。This includes the resources referenced and contained by the solution which defines how the resource is managed after the solution is installed. 解决方案中的每个资源应在 referencedResourcescontainedResources 属性中列出。Each resource in the solution should be listed in either the referencedResources or the containedResources property.

属性Property 说明Description
workspaceResourceIdworkspaceResourceId 格式为 <Resource Group ID>/providers/Microsoft.OperationalInsights/workspaces/<Workspace Name> 的 Log Analytics 工作区的 ID。ID of the Log Analytics workspace in the form <Resource Group ID>/providers/Microsoft.OperationalInsights/workspaces/<Workspace Name>.
referencedResourcesreferencedResources 解决方案中不应随解决方案一起删除的资源的列表。List of resources in the solution that should not be removed when the solution is removed.
containedResourcescontainedResources 解决方案中应随解决方案一起删除的资源的列表。List of resources in the solution that should be removed when the solution is removed.

上面的示例适用于具有 runbook、计划和视图的解决方案。The example above is for a solution with a runbook, a schedule, and view. properties 元素中引用 计划和 runbook,这样它们就不会随解决方案一起删除。The schedule and runbook are referenced in the properties element so they are not removed when the solution is removed. 包含视图,以便它将随解决方案一起删除。The view is contained so it is removed when the solution is removed.

计划Plan

解决方案资源的 plan 实体具有下表中的属性。The plan entity of the solution resource has the properties in the following table.

属性Property 说明Description
namename 解决方案名称。Name of the solution.
版本version 由作者确定的解决方案版本。Version of the solution as determined by the author.
productproduct 标识解决方案的唯一字符串。Unique string to identify the solution.
publisherpublisher 解决方案发布者。Publisher of the solution.

后续步骤Next steps