az-hop/bicep/vm.bicep

154 строки
4.4 KiB
Bicep
Исходник Постоянная ссылка Обычный вид История

2022-12-15 21:05:12 +03:00
targetScope = 'resourceGroup'
param name string
param vm object
param image object
2023-05-10 18:41:46 +03:00
param location string
2022-12-15 21:05:12 +03:00
param resourcePostfix string = '${uniqueString(subscription().subscriptionId, resourceGroup().id)}x'
param subnetId string
param adminUser string
@secure()
param adminPassword string
@secure()
param adminSshPublicKey string
param asgIds object
2022-12-15 21:05:12 +03:00
2023-05-10 18:41:46 +03:00
resource publicIp 'Microsoft.Network/publicIPAddresses@2022-07-01' = if (contains(vm, 'pip') && vm.pip) {
name: '${name}-pip'
2022-12-15 21:05:12 +03:00
location: location
sku: {
name: 'Standard'
2022-12-15 21:05:12 +03:00
}
properties: {
2023-05-04 13:24:50 +03:00
publicIPAllocationMethod: 'Static'
2022-12-15 21:05:12 +03:00
publicIPAddressVersion: 'IPv4'
idleTimeoutInMinutes: 4
dnsSettings: {
domainNameLabel : '${name}${resourcePostfix}'
2022-12-15 21:05:12 +03:00
}
}
}
2023-02-13 20:07:46 +03:00
// var count = contains(vm, 'count') && vm.count > 1 ? vm.count : 1
// var vmPrefixes = [ for i in range(0, count): count > 1 ? '-${(i + 1)}' : '' ]
2023-05-10 18:41:46 +03:00
resource nic 'Microsoft.Network/networkInterfaces@2022-07-01' = {
2023-02-13 20:07:46 +03:00
name: '${name}-nic'
2022-12-15 21:05:12 +03:00
location: location
properties: {
ipConfigurations: [
{
2023-02-13 20:07:46 +03:00
name: '${name}-ipconfig'
properties: union(
{
applicationSecurityGroups: map(vm.asgs, asg => { id: asgIds[asg] })
subnet: {
id: subnetId
}
privateIPAllocationMethod: 'Dynamic'
}, contains(vm, 'pip') && vm.pip ? {
2023-03-28 13:01:09 +03:00
publicIpAddress: {
id: publicIp.id
}
2022-12-15 21:05:12 +03:00
} : {}
)
2022-12-15 21:05:12 +03:00
}
]
}
2023-02-13 20:07:46 +03:00
}
2022-12-15 21:05:12 +03:00
var datadisks = contains(vm, 'datadisks') ? vm.datadisks : []
2022-12-15 21:05:12 +03:00
2023-05-10 18:41:46 +03:00
resource virtualMachine 'Microsoft.Compute/virtualMachines@2022-11-01' = {
2023-02-13 20:07:46 +03:00
name: name
2022-12-15 21:05:12 +03:00
location: location
2023-02-06 13:14:05 +03:00
plan: contains(image, 'plan') && empty(image.plan) == false ? {
publisher: split(image.plan,':')[0]
product: split(image.plan,':')[1]
name: split(image.plan,':')[2]
2022-12-15 21:05:12 +03:00
} : null
2023-02-09 20:49:11 +03:00
identity: {
type: 'SystemAssigned'
}
2022-12-15 21:05:12 +03:00
properties: {
hardwareProfile: {
vmSize: vm.sku
2022-12-15 21:05:12 +03:00
}
storageProfile: {
dataDisks: [ for (disk, idx) in datadisks: union({
2022-12-15 21:05:12 +03:00
name: disk.name
managedDisk: {
storageAccountType: disk.disksku
}
lun: idx
createOption: disk.createOption
},
disk.createOption == 'FromImage' ? {} : {diskSizeGB: disk.size},
contains(disk, 'caching') ? {
caching: disk.caching
} : {}
2022-12-15 21:05:12 +03:00
)]
osDisk: union(
{
createOption: 'FromImage'
managedDisk: {
storageAccountType: vm.osdisksku
}
caching: 'ReadWrite'
}, contains(vm, 'osdisksize') ? {
diskSizeGB: vm.osdisksize
} : {}
2022-12-15 21:05:12 +03:00
)
imageReference: image.ref
2022-12-15 21:05:12 +03:00
}
networkProfile: {
networkInterfaces: [
{
2023-02-13 20:07:46 +03:00
id: nic.id
2022-12-15 21:05:12 +03:00
}
]
}
osProfile: union(
{
2023-02-13 20:07:46 +03:00
computerName: name
adminUsername: adminUser
}, contains(vm, 'deploy_script') && vm.deploy_script != '' ? { // deploy script not empty
customData: base64(vm.deploy_script)
} : {}, contains(vm, 'windows') && vm.windows == true ? { // windows
adminPassword: adminPassword
windowsConfiguration: {
winRM: {
listeners: [
{
protocol: 'Http'
}
]
}
}
} : {}, ! contains(vm, 'windows') || vm.windows == false ? { // linux
linuxConfiguration: {
disablePasswordAuthentication: true
ssh: {
publicKeys: [
{
path: '/home/${adminUser}/.ssh/authorized_keys'
keyData: adminSshPublicKey
}
]
}
}
} : {}, contains(vm, 'ahub') && vm.ahub == true ? { // ahub
licenseType: 'Windows_Server'
} : {}
)
2022-12-15 21:05:12 +03:00
}
2023-02-13 20:07:46 +03:00
}
2022-12-15 21:05:12 +03:00
//output private_ip string = nic.properties.ipConfigurations[0].properties.privateIPAddress
output fqdn string = contains(vm, 'pip') && vm.pip ? publicIp.properties.dnsSettings.fqdn : ''
2023-06-26 16:41:26 +03:00
output publicIp string = contains(vm, 'pip') && vm.pip ? publicIp.properties.ipAddress : ''
2023-02-13 20:07:46 +03:00
output privateIp string = nic.properties.ipConfigurations[0].properties.privateIPAddress
output principalId string = virtualMachine.identity.principalId
//output privateIps array = [ for i in range(0, count): nic[i].properties.ipConfigurations[0].properties.privateIPAddress ]
//output principalIds array = [ for i in range(0, count): virtualMachine[i].identity.principalId ]