Ontwikkelomgeving configureren voor implementatiescripts in ARM-sjablonen

Meer informatie over hoe u een ontwikkelomgeving maakt voor het ontwikkelen en testen van implementatiescripts voor ARM-sjabloonimplementaties met een installatiekopie van een implementatiescript. U kunt een Azure-containerinstantie maken of Docker gebruiken. Beide opties worden behandeld in dit artikel.

Belangrijk

Implementatiescriptlogs kunnen inhoud bevatten die geschreven is naar Write-Host, echo, stdout, en stderr. Je kunt deze informatie ophalen via het /deploymentScripts/logs endpoint of gerelateerde API's. Schrijf geen gevoelige informatie naar de scriptuitvoer, inclusief access tokens, bearer tokens, SAS-tokens, verbindingsstrings, inloggegevens of andere geheimen. Scriptauteurs zijn verantwoordelijk voor het waarborgen dat implementatiescriptlogs geen gevoelige informatie blootleggen.

Vereisten

Azure PowerShell-container

Als u geen Azure PowerShell-implementatiescript hebt, kunt u een hello.ps1-bestand maken met behulp van de volgende inhoud:

param([string] $name)
$output = 'Hello {0}' -f $name
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output

Azure CLI-container

Voor een Azure CLI-containerimage kunt u een bestand met de naam hello.sh maken met de volgende inhoud:

FIRSTNAME=$1
LASTNAME=$2
OUTPUT="{\"name\":{\"displayName\":\"$FIRSTNAME $LASTNAME\",\"firstName\":\"$FIRSTNAME\",\"lastName\":\"$LASTNAME\"}}"
echo -n "Hello "
echo $OUTPUT | jq -r '.name.displayName'

Notitie

Wanneer u een Azure CLI-implementatiescript uitvoert, slaat een omgevingsvariabele met de naam AZ_SCRIPTS_OUTPUT_PATH de locatie van het scriptuitvoerbestand op. De omgevingsvariabele is niet beschikbaar in de container voor de ontwikkelomgeving. Zie Werken met uitvoer van Azure CLI-script voor meer informatie over het werken met uitvoer van Azure CLI.

Azure PowerShell-containerinstantie gebruiken

Als u uw scripts op uw computer wilt maken, moet u een opslagaccount maken en het opslagaccount koppelen aan de containerinstantie. U kunt uw script dus uploaden naar het opslagaccount en het script uitvoeren op de containerinstantie.

Notitie

Het opslagaccount dat u maakt om uw script te testen, is niet hetzelfde opslagaccount dat door de implementatiescriptservice wordt gebruikt om het script uit te voeren. De service voor implementatiescripts maakt bij elke uitvoering een unieke naam voor een bestandsshare.

Een Azure PowerShell-containerinstantie maken

Met de volgende Azure Resource Manager-sjabloon (ARM-sjabloon) maakt u een containerinstantie en een bestandsshare, en koppelt u vervolgens de bestandsshare aan de containerimage.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specify a project name that is used for generating resource names."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specify the resource location."
      }
    },
    "containerImage": {
      "type": "string",
      "defaultValue": "mcr.microsoft.com/azuredeploymentscripts-powershell:az9.7",
      "metadata": {
        "description": "Specify the container image."
      }
    },
    "mountPath": {
      "type": "string",
      "defaultValue": "/mnt/azscripts/azscriptinput",
      "metadata": {
        "description": "Specify the mount path."
      }
    }
  },
  "variables": {
    "storageAccountName": "[toLower(format('{0}store', parameters('projectName')))]",
    "fileShareName": "[format('{0}share', parameters('projectName'))]",
    "containerGroupName": "[format('{0}cg', parameters('projectName'))]",
    "containerName": "[format('{0}container', parameters('projectName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
      "apiVersion": "2025-06-01",
      "name": "[format('{0}/default/{1}', variables('storageAccountName'), variables('fileShareName'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2025-09-01",
      "name": "[variables('containerGroupName')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[parameters('containerImage')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGB": "[json('1.5')]"
                }
              },
              "ports": [
                {
                  "protocol": "TCP",
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "filesharevolume",
                  "mountPath": "[parameters('mountPath')]"
                }
              ],
              "command": [
                "/bin/sh",
                "-c",
                "pwsh -c 'Start-Sleep -Seconds 1800'"
              ]
            }
          }
        ],
        "osType": "Linux",
        "volumes": [
          {
            "name": "filesharevolume",
            "azureFile": {
              "readOnly": false,
              "shareName": "[variables('fileShareName')]",
              "storageAccountName": "[variables('storageAccountName')]",
              "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2023-01-01').keys[0].value]"
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    }
  ]
}

De standaardwaarde voor het aankoppelpad is /mnt/azscripts/azscriptinput. Dit is het pad in de containerinstantie waar deze aan de bestandsshare is gekoppeld.

De standaardcontainerinstallatiekopie die in de sjabloon is opgegeven, is mcr.microsoft.com/azuredeploymentscripts-powershell:az9.7. Bekijk een lijst met alle ondersteunde Azure PowerShell-versies.

De sjabloon onderbreekt de containerinstantie na 1800 seconden. U hebt 30 minuten voordat de containerinstantie de status Beëindigd krijgt en de sessie wordt beëindigd.

De sjabloon implementeren:

$projectName = Read-Host -Prompt "Enter a project name that is used to generate resource names"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateFile = Read-Host -Prompt "Enter the template file path and file name"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroup -Location $location -name $resourceGroupName
New-AzResourceGroupDeployment -resourceGroupName $resourceGroupName -TemplateFile $templatefile -projectName $projectName

Het implementatiescript uploaden

Upload uw implementatiescript naar het opslagaccount. Hier volgt een voorbeeld van een PowerShell-script:

$projectName = Read-Host -Prompt "Enter the same project name that you used earlier"
$fileName = Read-Host -Prompt "Enter the deployment script file name with the path"

$resourceGroupName = "${projectName}rg"
$storageAccountName = "${projectName}store"
$fileShareName = "${projectName}share"

$context = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
Set-AzStorageFileContent -Context $context -ShareName $fileShareName -Source $fileName -Force

U kunt het bestand ook uploaden met behulp van Azure Portal of de Azure CLI.

Het implementatiescript testen

  1. Open in Azure Portal de resourcegroep waarin u de containerinstantie en het opslagaccount hebt geïmplementeerd.

  2. Open de containergroep. De standaardnaam van de containergroep is de projectnaam die is toegevoegd aan cg. De containerinstantie heeft de status Actief .

  3. Selecteer in het resourcemenu Containers. De naam van het containerexemplaar is de projectnaam met daaraan toegevoegd container.

    Schermopname van de optie in het implementatiescript om een containerinstantie te verbinden in de Azure-portal.

  4. Selecteer Verbinding maken en selecteer vervolgens Verbinding maken. Als u geen verbinding kunt maken met de containerinstantie, start u de containergroep opnieuw en probeert u het opnieuw.

  5. Voer in het consolevenster de volgende opdrachten uit:

    cd /mnt/azscripts/azscriptinput
    ls
    pwsh ./hello.ps1 "John Dole"
    

    De uitvoer is Hello John Dole.

    Schermopname van de testuitvoer voor het verbinden van het containerexemplaren van het implementatiescript die wordt weergegeven in de console.

Een Azure CLI-containerinstantie gebruiken

Als u uw scripts op uw computer wilt maken, maakt u een opslagaccount en koppelt u het opslagaccount aan de containerinstantie. Vervolgens kunt u uw script uploaden naar het opslagaccount en het script uitvoeren op de containerinstantie.

Notitie

Het opslagaccount dat u maakt om uw script te testen, is niet hetzelfde opslagaccount dat door de implementatiescriptservice wordt gebruikt om het script uit te voeren. De service voor implementatiescripts maakt bij elke uitvoering een bestandsshare met een unieke naam.

Een Azure CLI-containerinstantie maken

De volgende ARM-sjabloon maakt een containerinstantie en een bestandsshare en koppelt de bestandsshare vervolgens aan de containerimage:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specify a project name that is used for generating resource names."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specify the resource location."
      }
    },
    "containerImage": {
      "type": "string",
      "defaultValue": "mcr.microsoft.com/azure-cli:2.9.1",
      "metadata": {
        "description": "Specify the container image."
      }
    },
    "mountPath": {
      "type": "string",
      "defaultValue": "/mnt/azscripts/azscriptinput",
      "metadata": {
        "description": "Specify the mount path."
      }
    }
  },
  "variables": {
    "storageAccountName": "[toLower(format('{0}store', parameters('projectName')))]",
    "fileShareName": "[format('{0}share', parameters('projectName'))]",
    "containerGroupName": "[format('{0}cg', parameters('projectName'))]",
    "containerName": "[format('{0}container', parameters('projectName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-06-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
      "apiVersion": "2025-06-01",
      "name": "[format('{0}/default/{1}', variables('storageAccountName'), variables('fileShareName'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2025-09-01",
      "name": "[variables('containerGroupName')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[parameters('containerImage')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGB": "[json('1.5')]"
                }
              },
              "ports": [
                {
                  "protocol": "TCP",
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "filesharevolume",
                  "mountPath": "[parameters('mountPath')]"
                }
              ],
              "command": [
                "/bin/bash",
                "-c",
                "echo hello; sleep 1800"
              ]
            }
          }
        ],
        "osType": "Linux",
        "volumes": [
          {
            "name": "filesharevolume",
            "azureFile": {
              "readOnly": false,
              "shareName": "[variables('fileShareName')]",
              "storageAccountName": "[variables('storageAccountName')]",
              "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2022-09-01').keys[0].value]"
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    }
  ]
}

De standaardwaarde voor het mountpad is /mnt/azscripts/azscriptinput. Dit is het pad in de containerinstantie waar deze aan de bestandsshare is gekoppeld.

De standaardcontainerinstallatiekopie die in de sjabloon is opgegeven, is mcr.microsoft.com/azure-cli:2.9.1. Bekijk een lijst met ondersteunde Azure CLI-versies.

Belangrijk

Het implementatiescript maakt gebruik van de beschikbare CLI-installatiekopieën van Microsoft Container Registry (MCR). Het duurt ongeveer één maand om een CLI-image te certificeren voor een deployscript. Gebruik niet de CLI-versies die binnen 30 dagen zijn uitgebracht. Raadpleeg de releaseopmerkingen van Azure CLI om de releasedatums voor de installatiekopieën te vinden. Als u een niet-ondersteunde versie gebruikt, worden in het foutbericht de ondersteunde versies vermeld.

De sjabloon onderbreekt de containerinstantie na 1800 seconden. U hebt 30 minuten voordat de containerinstantie de terminalstatus krijgt en de sessie wordt beëindigd.

De sjabloon implementeren:

$projectName = Read-Host -Prompt "Enter a project name that is used to generate resource names"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateFile = Read-Host -Prompt "Enter the template file path and file name"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroup -Location $location -name $resourceGroupName
New-AzResourceGroupDeployment -resourceGroupName $resourceGroupName -TemplateFile $templatefile -projectName $projectName

Het implementatiescript uploaden

Upload uw implementatiescript naar het opslagaccount. Hier volgt een PowerShell-voorbeeld:

$projectName = Read-Host -Prompt "Enter the same project name that you used earlier"
$fileName = Read-Host -Prompt "Enter the deployment script file name with the path"

$resourceGroupName = "${projectName}rg"
$storageAccountName = "${projectName}store"
$fileShareName = "${projectName}share"

$context = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
Set-AzStorageFileContent -Context $context -ShareName $fileShareName -Source $fileName -Force

U kunt het bestand ook uploaden met behulp van Azure Portal of de Azure CLI.

Het implementatiescript testen

  1. Open in Azure Portal de resourcegroep waarin u de containerinstantie en het opslagaccount hebt geïmplementeerd.

  2. Open de containergroep. De standaardnaam van de containergroep is de projectnaam die is toegevoegd aan cg. De containerinstantie wordt weergegeven in de status Actief .

  3. Selecteer Containers in het menu Resources. De naam van het containerexemplaar is de projectnaam met daaraan toegevoegd container.

    Schermafbeelding van de optie voor het verbinden van een containerinstantie in het implementatiescript in de Azure-portal.

  4. Selecteer Verbinding maken en selecteer vervolgens Verbinding maken. Als u geen verbinding kunt maken met de containerinstantie, start u de containergroep opnieuw en probeert u het opnieuw.

  5. Voer in het consolevenster de volgende opdrachten uit:

    cd /mnt/azscripts/azscriptinput
    ls
    ./hello.sh John Dole
    

    De uitvoer is Hello John Dole.

    Schermopname van de testuitvoer van het containerexemplaren van het implementatiescript die wordt weergegeven in de console.

Docker gebruiken

U kunt een vooraf geconfigureerde Docker-containerimage gebruiken als ontwikkelomgeving voor het ontwikkelen van deploymentscripts. Zie Docker downloaden om Docker te installeren. U moet ook het delen van bestanden configureren om de map te koppelen, die de implementatiescripts bevat in de Docker-container.

  1. Haal de containerinstallatiekopie van het implementatiescript op de lokale computer:

    docker pull mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    In het voorbeeld wordt versie PowerShell 4.3.0 gebruikt.

    Een CLI-image ophalen vanuit een MCR:

    docker pull mcr.microsoft.com/azure-cli:2.0.80
    

    In dit voorbeeld wordt versie CLI 2.0.80 gebruikt. Implementatiescript maakt gebruik van de standaard-CLI-containerinstallatiekopieën die hier worden gevonden.

  2. Voer de Docker-image lokaal uit.

    docker run -v <host drive letter>:/<host directory name>:/data -it mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    Vervang <hoststationsletter> en <hostmapnaam door een bestaande map op de gedeelde schijf. Het koppelt de map aan de map /data in de container. Bijvoorbeeld om D:\docker toe te wijzen:

    docker run -v d:/docker:/data -it mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    -it betekent dat de containerimage actief wordt gehouden.

    Een CLI-voorbeeld:

    docker run -v d:/docker:/data -it mcr.microsoft.com/azure-cli:2.0.80
    
  3. In de volgende schermopname ziet u hoe u een PowerShell-script uitvoert, ervan uitgaande dat u een helloworld.ps1-bestand op de gedeelde schijf hebt.

    Schermopname van het Resource Manager-sjabloonimplementatiescript met behulp van de Docker-opdracht.

Nadat het script is getest, kunt u het gebruiken als een implementatiescript in uw sjablonen.

Volgende stappen

In dit artikel hebt u geleerd hoe u implementatiescripts gebruikt. Een zelfstudie over een implementatiescript doorlopen: