设置ARM模板Web appSetting

时间:2016-11-03 07:41:56

标签: powershell azure

如何使用ARM将应用程序设置部署到网站?

1 在VS ARM Deploy json中运行以下内容时:

....
     "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[variables('hostingPlanName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "HostingPlan"
      },
      "sku": {
        "name": "[variables('skuName')]",
        "capacity": "[variables('skuCapacity')]"
      },
      "properties": {
        "name": "[variables('hostingPlanName')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "[variables('webSiteName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Web/serverFarms/', variables('hostingPlanName'))]"
      ],
      "tags": {
        "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', variables('hostingPlanName'))]": "empty",
        "displayName": "Website"
      },
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
      },
      "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "web",
          "type": "config",
          "dependsOn": [
            "[concat('Microsoft.Web/Sites/', variables('webSiteName'))]"
          ],
          "properties": {
            "netFrameworkVersion": "4.5.2",
            "use32BitWorkerProcess": true,
            "webSocketsEnabled": false,
            "alwaysOn": false,
            "requestTracingEnabled": true,
            "httpLoggingEnabled": true,
            "logsDirectorySizeLimit": 40,
            "detailedErrorLoggingEnabled": true,
            "appSettings": [
              {
                "name": "testn",
                "value": "testv"
              }
            ],
            "connectionstrings": [
              {
                "name": "testn",
                "value": "testv",
                "type": "SQLServer"
              }
            ]
          }
        }
      ]
    }
  ],

输出是;

VERBOSE: Performing the operation "Creating Deployment" on target "testdeploy3".
VERBOSE: 4:44:42 PM - Template is valid.
...
VERBOSE: 4:45:17 PM - Resource Microsoft.Web/sites/config 'testwebadtzmdritygpo/web' provisioning status is succeeded

...
ProvisioningState       : Succeeded

在Portal中进行双重检查,显示尚未创建appsettings和connectionstrings

问题1:如何使用PowerShell查询网站的配置属性?

问题2:如何使用ARM将应用程序设置部署到网站?

2 个答案:

答案 0 :(得分:4)

感谢@ Alexander-s和@davidebbo

使用PowerShell

部署(没有appsettings)成功后。然后运行此命令以完全覆盖appsettings。

$subscriptionID = {...}
Add-AzureRmAccount
Set-AzureRmContext -SubscriptionID $subscriptionID

# List appsettings
$resource = Invoke-AzureRmResourceAction -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Web/sites/config -ResourceName <webSiteName>/appsettings -Action list -ApiVersion 2015-08-01 -Force
$resource.Properties

# SET list
$appsettingTest1Value = "testValue1"
$appsettingTest2Value = "testValue2"
$PropertiesObject = @{
    appsettingTest1=$appsettingTest1Value,
    appsettingTest2=$appsettingTest2Value,
    WEBSITE_NODE_DEFAULT_VERSION: "4.4.7"
  }
New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName <ResourceGroupName> -ResourceType Microsoft.Web/sites/config -ResourceName <webSiteName>/appsettings -ApiVersion 2015-08-01 -Force

使用ARM部署JSON

{
  "apiVersion": "2015-08-01",
  "name": "[variables('webSiteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[concat('Microsoft.Web/serverFarms/', parameters('hostingPlanName'))]",
    "[concat('Microsoft.Storage/storageAccounts/', variables('storage_account_name'))]",
    "[resourceId('Microsoft.Sql/servers', variables('sqlserverName'))]",
    "[resourceId('Microsoft.Sql/servers/databases', variables('sqlserverName'), parameters('database_name_auth'))]"
  ],
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "empty",
    "displayName": "Website"
  },
  "properties": {
    "name": "[variables('webSiteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "web",
      "type": "config",
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('webSiteName'))]"
      ],
      "properties": {
        "netFrameworkVersion": "[parameters('dotnet_version')",
        "use32BitWorkerProcess": "[parameters('use32bit_worker_process')",
        "webSocketsEnabled": false,
        "alwaysOn": "[parameters('enable_always_on')]",
        "requestTracingEnabled": true,
        "httpLoggingEnabled": true,
        "logsDirectorySizeLimit": 40,
        "detailedErrorLoggingEnabled": true
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "appsettings",
      "type": "config",
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('webSiteName'))]"
      ],
      "properties": {
        "appsettingTest1": "[parameters('appsettingTest1Value')]",
        "appsettingTest2": "[parameters('appsettingTest2Value')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "connectionstrings",
      "type": "config",
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('webSiteName'))]"
      ],
      "properties": {
        "dbconnstringTest1": {
          "value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('database_name_auth'), ';User Id=', parameters('administratorLogin'), '@', variables('sqlserverName'), ';Password=', parameters('administratorLoginPassword'), ';')]",
          "type": "SQLServer"
        },
        "AzureWebJobsConnectionString": {
          "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storage_account_name'),';AccountKey=',listkeys(resourceId('Microsoft.Storage/storageAccounts', variables('storage_account_name')), '2015-05-01-preview').key1,';')]",
          "type": "Custom"
        }
      }
    }
  ]
},

答案 1 :(得分:1)

回答1

   $webapp = Get-AzureRmWebApp -Name $webSiteName
   $webapp.SiteConfig.AppSettings

回答2

这是我在ARM模板中的内容,它运行正常:

  "resources": [
    {
      "apiVersion": "2015-08-01",
      "location": "[resourceGroup().location]",
      "type": "config",
      "name": "appsettings",
      "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
      ],
      "properties": {...}
    },
    {
      "apiVersion": "2015-08-01",
      "location": "[resourceGroup().location]",
      "type": "config",
      "name": "web",
      "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
      ],
      "properties": {
        "alwaysOn": true
      }]

如您所见,我将AppSettings设置为网站下的单独资源。试试吧,应该工作。

希望它有所帮助。