Olá pessoal,
Dando sequência ao nosso minicurso de Bicep, vamos ver hoje algumas técnicas avançadas como laços de repetição, dicionarios, etc.
Video:
Comando para deployment:
$AzResourceGroupDeployment= @{
Name = 'BicepTest'
ResourceGroupName = 'BicepLab'
Mode = 'Incremental'
TemplateFile = '.\Aula4.bicep'
TemplateParameterFile = '.\Aula4.parameters.json'
}
New-AzResourceGroupDeployment @AzResourceGroupDeployment
Template:
param location string
@allowed([
'Web'
'Banco'
])
param ServerType string
@minValue(1)
@maxValue(25)
param NumberOfInstances int
@secure()
param adminPassword string
param adminUsername string = 'bicepadmin'
param tags object = {}
var VMParameters = {
Web: {
namePrefix: 'AZNPDWEB'
vmSize:'Standard_B1s'
}
Banco:{
namePrefix: 'AZNPDDBS'
vmSize: 'Standard_B1s'
}
}
var virtualNetworkName = 'BicepLab'
var subnetName = 'Subnet-1'
resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' existing = {
name: virtualNetworkName
}
resource subnetRef 'Microsoft.Network/virtualNetworks/subnets@2021-05-01' existing = {
name: subnetName
parent: vnet
}
resource networkInterfaces 'Microsoft.Network/networkInterfaces@2021-03-01' = [for i in range(1, NumberOfInstances):{
name: '${VMParameters[ServerType].nameprefix}${padLeft(i,2,'0')}-nic'
location: location
properties: {
ipConfigurations: [
{
name: 'ipconfig'
properties: {
subnet: {
id: subnetRef.id
}
privateIPAllocationMethod: 'Dynamic'
}
}
]
enableAcceleratedNetworking: false
}
tags: tags
}]
resource dataDisks 'Microsoft.Compute/disks@2021-04-01' = [for i in range(1, NumberOfInstances):{
name: '${VMParameters[ServerType].nameprefix}${padLeft(i,2,'0')}-datadisk'
location: location
sku: {
name: 'Premium_LRS'
}
properties: {
diskSizeGB: 123
encryption:{
type: 'EncryptionAtRestWithPlatformKey'
}
creationData: {
createOption: 'Empty'
}
}
tags: tags
}]
resource virtualMachines 'Microsoft.Compute/virtualMachines@2021-07-01' = [for i in range(0, NumberOfInstances):{
name: '${VMParameters[ServerType].nameprefix}${padLeft((i+1),2,'0')}'
location: location
tags: tags
properties: {
hardwareProfile: {
vmSize: VMParameters[ServerType].vmSize
}
storageProfile: {
osDisk: {
createOption: 'FromImage'
name: '${VMParameters[ServerType].nameprefix}${padLeft((i+1),2,'0')}-osdisk'
osType: 'Windows'
managedDisk: {
storageAccountType: 'Premium_LRS'
}
}
imageReference: {
publisher: 'MicrosoftWindowsServer'
offer: 'WindowsServer'
sku: '2019-datacenter-gensecond'
version: 'latest'
}
dataDisks: [
{
createOption: 'Attach'
lun: 0
caching: 'None'
managedDisk: {
id: dataDisks[i].id
}
}
]
}
networkProfile: {
networkInterfaces: [
{
id: networkInterfaces[i].id
}
]
}
osProfile: {
computerName: '${VMParameters[ServerType].nameprefix}${padLeft((i+1),2,'0')}'
adminUsername: adminUsername
adminPassword: adminPassword
windowsConfiguration: {
enableAutomaticUpdates: true
provisionVMAgent: true
patchSettings: {
enableHotpatching: false
patchMode: 'AutomaticByOS'
}
}
}
diagnosticsProfile: {
bootDiagnostics: {
enabled: true
}
}
}
}]
Parametros:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "canadacentral"
},
"ServerType": {
"value": "Web"
},
"NumberOfInstances": {
"value": 3
},
"adminPassword": {
"value": "S3nh4S3cret@!!"
},
"adminUsername": {
"value": "bicepadmin"
},
"tags": {
"value": {
"Curso": "Azure Bicep"
}
}
}
}
Dúvidas? Sugestões? Comente!
Até a próxima!