系统极客一直在努力
专注操作系统及软件使用技能

创建Azure Resource Manager模板 – 3.参数

Azure Resource Manager

在本系列的前两篇文章中,我们简要地介绍了下 ARM 模板的不同部署方法ARM 模板语言基础。从本篇文章起,我们将开始创建一个 Azure Resource Manager 模板。



虽然在我们的文章中无法介绍并涵盖 Azure 中的所有资源类型,但还是会向大家介绍 ARM 模板语言的功能和表示的各个方面,同时也会帮助大家一起总结 ARM 模板的最佳实践。

在我们文章所演示 ARM 模板创建的测试场景中大概会涉及到如下 Azure 资源:

  1. 用于存放不同类型 Azure 资源的「资源组」
  2. 用于存储 VM 系统盘和数据磁盘的「存储账户」
  3. 建立虚拟机之间连接的「虚拟网络」
  4. 「公共IP地址」用于连接 Internet
  5. 用于 RDP 或 Powershell 远程连接的「负载平衡器」
  6. 各虚拟机的「网络接口」,就是网卡
  7. 一台运行 DNS 服务的域控制器

arm-templates-parameters-2

以上环境并不是一个复杂的部署场景,下面我们就一步步开始介绍。

创建资源组

如前所述,ARM 模板的部署需要指定「资源组」,所以我们先创建一个。

Login-AzureRmAccount
New-AzureRmResourceGroup -Name SysGeekARMDemo -Location 'East Asia'

注意:ARM 方式的资源部署需要使用 Azure PowerShell 1.0 cmdlets。

添加参数

在创建好「资源组」后,我们便开始正式创建 ARM 模板。首先要创建的是「存储账户」,Azure 存储账户有 nametypelocation 3 个属性可供管理员配置。在部署之前大家最好先确定好要使用的 name 自定义名称和 type 存储类型值,这是我们将在 ARM 模板中所使用的参数。下面我们就开始定义:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountName": {
            "type": "string",
            "defaultValue": "myARMDemo",
            "minLength": 3,
            "maxLength": 24,
            "metadata": {
                "description": "Unique name for the storage account."
            }
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS",
                "Standard_RAGRS"
            ],
            "metadata": {
                "description": "Account type based on the storage redundancy requirements."
            }
        }
    }
}

在上面的模板中我们加入了 $schemacontentVersion 两个强制性元素子元素,在 parameters 中我们创建「存储账户」的 2 个参数,下面是语法:

"parameters": {
   "" : {
     "type" : "",
     "defaultValue": "",
     "allowedValues": [ "" ],
     "minValue":,
     "maxValue":,
     "minLength":,
     "maxLength":,
     "metadata": {
         "description": "" 
     }
   }
}

storageAccountName 是字符串类型,我们将其 defaultValue (默认值)设置为「myARMDemo」,类型属性中有效的 JSON 类型可以为:

  • string 或 secureString
  • int
  • bool
  • object 或 secureObject
  • array

Azure 存储账户名称必需为 3 – 24 个字符之间,因此我们通过 minLengthmaxLength 两个属性进行约束。对于 storageAccountType 存储账户类型值,我们通过 allowedValues 进行提前定义,在部署时通过下拉列表进行选择。在 Azure 中部署「存储账户」时,类型是必需定义好的属性,我们可以使用 New-AzureRmResourceGroupDeployment cmdlet 来查看到 Azure 对「存储账户」的定义。

New-AzureRmResourceGroupDeployment

添加资源

为了成功部署模板,我们至少需要将一种资源类型添加到 resources 元素集合,我们继续添加「存储账户」的存储资源类型:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountName": {
            "type": "string",
            "defaultValue": "MyARMDemo",
            "minLength": 3,
            "maxLength": 24,
            "metadata": {
                "description": "Unique name for the storage account."
            }
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS",
                "Standard_RAGRS"
            ],
            "metadata": {
                "description": "Account type based on the storage redundancy requirements."
            }
        }
    },
    "resources": [
        {
            "name": "[parameters('storageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "location": "[ResourceGroup().Location]",
            "apiVersion": "2015-05-01-preview",
            "properties": {
                "accountType": "[parameters('storageAccountType')]"
            }
        }        
    ]
}

我们在前面的文章中就提到的,resources 元素在 ARM 模板是一个 JSON 数组。它是以逗号分隔的 JSON 对象,其中每个 JSON 对象表示资源类型实例的集合。作为资源类型,需要通过 name 属性来定义资源实例,我们将检索到的 storageAccountName 参数值分配给存储账户资源的 name 属性。我们使用的是 parameters() 函数来检索参数名称的值,请注意 JSON 语法中的表达式需要写在方括号中。

"name": "[parameters('storageAccountName')]"

type 属性用于定义 Azure (Microsoft.Storage)资源提供程序和(storageAccounts)资源类型,location 属性值用于定义资源类型的部署区域。在我们的示例 ARM 模板中,使用了 resourceGroup() 函数来定义存储账户部署位置的 location 值。(该函数返回 id、name 和 location 共 3 个属性值)

"location": "[ResourceGroup().Location]"

properties 元素是用于标识特定属性的 JSON 对象,在示例中我们使用了 parameters() 函数来获取特定的  storageAccountType 存储账户类型值:

"properties": {
   "accountType": "[parameters('storageAccountType')]"
}

至此,我们的 ARM 模板已经可用于部署「存储账户」了,为了验证它是否能够正常工作,我们可以使用 PowerShell cmdlets 的 REST API 进行部署验证:

Test-AzureRmResourceGroupDeployment -ResourceGroupName myARMDemo -TemplateFile .\2-0_Template_Storage.json -storageAccountName myARMDemo -storageAccountType Standard_LRS –Verbose

一旦验证无误,便可以使用我们在本系列首篇文章中介绍的各种方法进行部署。如果使用 Azure Portal 进行模板部署的话,可以看到在部署时要求填写我们所定义好的 ARM 模板参数:

模板部署参数

赞(0) 赞赏

评论 1

  1. #1

    好东西~学习了。

    絮沫纷飞8年前 (2016-03-01)回复

微信赞赏