【问题标题】:Azure ARM Template DependentOn FileShareAzure ARM 模板 DependentOn 文件共享
【发布时间】:2021-01-10 05:35:23
【问题描述】:

我需要通过 ARM 模板存储帐户创建 --> 文件共享 --> 带有挂载文件共享的容器。
依赖关系是:

  1. 文件共享取决于存储帐户
  2. 容器依赖于文件共享

如何获取文件共享的ReferenceId?

我有以下代码:

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "storageAccountType": {
                "type": "string",
                "defaultValue": "Standard_LRS",
                "allowedValues": [
                    "Standard_LRS",
                    "Standard_GRS"
                ],
                "metadata": {
                    "description": "Storage account type (SKU)"
                }
            },
            "fileShareName": {
                "type": "string",
                "minLength": 3,
                "maxLength": 63,
                "defaultValue": "sftp",
                "metadata": {
                    "description": "Name of the File Share to be created. "
                }
            }
        },
        "variables": {
            "deploymentLocation": "[resourceGroup().location]",
            "storageAccountName": "[concat('sftpstr', uniqueString(resourceGroup().id))]"
        },
        "resources": [
            {
                "type": "Microsoft.Storage/storageAccounts",
                "name": "[variables('storageAccountName')]",
                "apiVersion": "2019-06-01",
                "location": "[variables('deploymentLocation')]",
                "sku": {
                    "name": "[parameters('storageAccountType')]"
                },
                "kind": "StorageV2",
                "properties": {
                    "accessTier": "Hot"
                }
            },
            {
                "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
                "apiVersion": "2019-06-01",
                "properties": {
                    "accessTier": "Hot"
                },
                "name": "[concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))]",
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
                ]
            }, 
            {
                "type": "Microsoft.ContainerInstance/containerGroups",
                "name": "sftp-container-group",
                "apiVersion": "2018-04-01",
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', ????? )]" //how to get reference to specific Fileshare??? I've tried >   [concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))] but it didn't work
                ],
                "properties": {
                    .......
                }
            }
        ] 
    }

我不知道如何设置对 Fileshare 的依赖:

"dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', ????? )]" //how to get reference to specific Fileshare??? 
],

我试过[concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))],但没用。

有什么想法吗?

谢谢

【问题讨论】:

  • 只需尝试将文件共享的名称改为“?????”在dependsOn 块中。
  • @CharlesXu 我做到了,但没用。我收到验证错误Deployment template validation failed: 'The template resource 'sftp-container-group' is not valid: Unable to evaluate template language function 'resourceId': the type 'Microsoft.Storage/storageAccounts/fileServices/shares' requires '3' resource name argument(s).

标签: azure dependencies azure-resource-manager arm-template fileshare


【解决方案1】:

使用以下格式:

"dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', variables('storageAccountName') , 'default', parameters('fileShareName') )]"
],

【讨论】:

【解决方案2】:

您可以创建两个单独的资源 Microsoft.Storage/storageAccounts/fileServicesMicrosoft.Storage/storageAccounts/fileServices/shares 并尝试像这样设置对 Fileshare 的依赖:

        "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', variables('storageAccountName'),  'default',  parameters('fileShareName') )]" 
        ],

或者,建议在容器实例中通过 azure-CLI 创建存储帐户和文件共享,并参考this quickstart template

 "variables": {
    "image": "microsoft/azure-cli",
    "cpuCores": "1.0",
    "memoryInGb": "1.5",
    "containerGroupName": "createshare-containerinstance",
    "containerName": "createshare"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[parameters('storageAccountName')]",
      "apiVersion": "2019-06-01",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2"
    },
    {
      "name": "[variables('containerGroupName')]",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2019-12-01",
      "location": "[parameters('containerInstanceLocation')]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
      ],
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[variables('image')]",
              "command": [
                "az",
                "storage",
                "share",
                "create",
                "--name",
                "[parameters('fileShareName')]"
              ],
              "environmentVariables": [
                {
                  "name": "AZURE_STORAGE_KEY",
                  "value": "[listKeys(parameters('storageAccountName'),'2019-06-01').keys[0].value]"
                },
                {
                  "name": "AZURE_STORAGE_ACCOUNT",
                  "value": "[parameters('storageAccountName')]"
                }
              ],
              "resources": {
                "requests": {
                  "cpu": "[variables('cpuCores')]",
                  "memoryInGb": "[variables('memoryInGb')]"
                }
              }
            }
          }
        ],
        "restartPolicy": "OnFailure",
        "osType": "Linux"
      }
    }
  ]

【讨论】:

    【解决方案3】:

    我发现了一些使它对我有用的东西:

    ACI 必须依赖于共享,否则它有时会在数量之前创建

    "dependsOn": ["resourceId('Microsoft.Storage/storageAccounts/fileServices/shares',variables('storage-account-name'), 'default', variables('file-share-name'))]"]
    

    由于某种原因,该共享无法作为子资源使用,因此我不得不将其设为完整资源

    {
        "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
        "apiVersion": "2021-04-01",
        "name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]",
        "properties":{
            "enabledProtocols": "SMB"
        },
        "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-account-name'))]"
        ]
    }
    

    另外,我必须注意资源 ID 的 default 部分

    "name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]"
    

    最后,ACI 无法正确连接,除非我明确启用 SMB 作为协议

    "properties":{
        "enabledProtocols": "SMB"
    }
    

    这是我用来在 Azure 容器实例中启动 NGINX 的模板,其中 Azure 文件共享作为已装载的卷。

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "containerGroups_name": {
                "defaultValue": "container-app",
                "type": "String"
            },
            "containerGroups_reverse_proxy_name": {
                "defaultValue": "app-proxy",
                "type": "String"
            },
            "acrPassword": {
                "type": "securestring"
            }
        },
        "variables": {
            "nginx-proxy-image": "nginx:stable",
            "storage-account-name": "[concat('storage', uniqueString(resourceGroup().name))]",
            "nginx-share-name": "nginx-share",
            "nginx-volume-name": "nginx-volume"
        },
        "resources": [
            {
                "type": "Microsoft.Storage/storageAccounts",
                "apiVersion": "2017-10-01",
                "name": "[variables('storage-account-name')]" ,
                "location": "[resourceGroup().location]",
                "kind": "StorageV2",
                "sku": {
                    "name": "Standard_LRS",
                    "tier": "Standard"
                }
            },
            {
                "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
                "apiVersion": "2021-04-01",
                "name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]",
                "properties":{
                    "enabledProtocols": "SMB"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-account-name'))]"
                ]
            },
            {
                "type": "Microsoft.ContainerInstance/containerGroups",
                "apiVersion": "2021-03-01",
                "name": "[parameters('containerGroups_name')]",
                "location": "[resourceGroup().location]",
                "properties": {
                    "sku": "Standard",
                    "containers": [
                        {
                            "name": "[parameters('containerGroups_reverse_proxy_name')]",
                            "properties" : {
                                "image": "[variables('nginx-proxy-image')]",
                                "ports": [
                                    {
                                        "protocol": "TCP",
                                        "port": 80
                                    },
                                    {
                                        "protocol": "TCP",
                                        "port": 433
                                    }
                                ],
                                "volumeMounts" : [
                                    {
                                        "name": "[variables('nginx-volume-name')]",
                                        "mountPath": "/etc/nginx"
                                    }
                                ],
                                "resources": {
                                    "requests":{
                                        "cpu": 1,
                                        "memoryInGB": 1.5
                                    }
                                }
                            }
                        }                    
                    ],
                    "initContainers": [],
                    "restartPolicy": "OnFailure",
                    "osType": "Linux",
                    "volumes": [
                        {
                            "name": "[variables('nginx-volume-name')]",
                            "azureFile": {
                                "shareName": "[variables('nginx-share-name')]",
                                "storageAccountName": "[variables('storage-account-name')]",
                                "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts',variables('storage-account-name')),'2017-10-01').keys[0].value]"
                            }
                        }
                    ]
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares',variables('storage-account-name'), 'default', variables('nginx-share-name'))]"
                ]
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2019-01-18
      • 2016-07-03
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多