Home > Net >  how to deploy two vm to two different resource group
how to deploy two vm to two different resource group

Time:01-15

Does anyone could give a hint on how should i do. i want to deploy 4 Az linux VM but 2 in each different resource group.

eg: rg-one will have 2 linux and rg-two will the other two

the way i did it is too create two block with azure_linux_virtual_machine, it is working but i was wondering if it could not be simplify.

thank in advance for the head up

here a snipped what i have done.

# Fetch exisiting resource_group
data "azurerm_resource_group" "rg-dock001" {
  name = var.resource_group01
}

# Fetch vm network
data "azurerm_virtual_network" "vm_network" {
  name                = var.vm_network
  resource_group_name = var.rg_name_network
}

output "azurerm_virtual_network" {
  value = data.azurerm_virtual_network.vm_network.id
}

# Fetch vm subnet 
data "azurerm_subnet" "vm_subnet" {
  name                 = var.vm_subnet
  resource_group_name  = var.rg_name_network
  virtual_network_name = var.vm_network
}

output "subnet_id" {
  value = data.azurerm_subnet.vm_subnet.id
}

resource "azurerm_network_interface" "ens124-01" {
  name                = var.vm_nic01[count.index]
  count               = length(var.vm_nic01)
  location            = var.rg_location
  resource_group_name = var.resource_group01

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.vm_subnet.id
    private_ip_address_allocation = "Static"
    private_ip_address            = "10.241.25.${count.index   10}"
  }

  tags = var.vm_tags
}
output "private_ip01" {
  value = length(azurerm_network_interface.ens124-01.*.private_ip_address)
}

# Fetch existing image 
data "azurerm_image" "custom_docker_image" {
  name_regex          = var.packer_image
  sort_descending     = true
  resource_group_name = var.resource_group_image
}
output "image_id" {
  value = data.azurerm_image.custom_docker_image.id
}

# create and display an SSH key
resource "tls_private_key" "ssh" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

output "tls_private_key" {
  value     = tls_private_key.ssh.private_key_pem
  sensitive = true
}

resource "azurerm_linux_virtual_machine" "main01" {
  name                            = var.vm_name01[count.index]
  count                           = length(var.vm_name01)
  resource_group_name             = var.resource_group01
  location                        = var.rg_location
  size                            = "standard_ds3_v2"
  admin_username                  = var.username
  admin_password                  = var.password
  disable_password_authentication = true
  network_interface_ids           = ["${element(azurerm_network_interface.ens124-01.*.id, count.index)}"]

  source_image_id = data.azurerm_image.custom_docker_image.id

  computer_name = var.vm_name01[count.index]

  admin_ssh_key {
    username   = var.ssh_username
    public_key = tls_private_key.ssh.public_key_openssh
  }

  os_disk {
    name                 = "disk-int-dock-0${count.index   1}"
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }

  tags = var.vm_tags
}


data "azurerm_resource_group" "rg-dock002" {
  name = var.resource_group02
}
resource "azurerm_network_interface" "ens124-02" {
  name                = var.vm_nic02[count.index]
  count               = length(var.vm_nic02)
  location            = var.rg_location
  resource_group_name = var.resource_group02

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.vm_subnet.id
    private_ip_address_allocation = "Static"
    private_ip_address            = "10.241.25.${count.index   20}"
  }

  tags = var.vm_tags
}
output "private_ip02" {
  value = length(azurerm_network_interface.ens124-02.*.private_ip_address)
}

resource "azurerm_linux_virtual_machine" "main02" {
  name                            = var.vm_name02[count.index]
  count                           = length(var.vm_name02)
  resource_group_name             = var.resource_group02
  location                        = var.rg_location
  size                            = "standard_ds3_v2"
  admin_username                  = var.username
  admin_password                  = var.password
  disable_password_authentication = true
  network_interface_ids           = ["${element(azurerm_network_interface.ens124-02.*.id, count.index)}"]

  source_image_id = data.azurerm_image.custom_docker_image.id

  computer_name = var.vm_name02[count.index]

  admin_ssh_key {
    username   = var.ssh_username
    public_key = tls_private_key.ssh.public_key_openssh
  }

  os_disk {
    name                 = "disk-int-dock-0${count.index   1}"
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }

  tags = var.vm_tags
}````

CodePudding user response:

As per your requirement you can use something like below :

provider "azurerm" {
    features {}
}

variable "VM" {
    default= {
        vm1={
            rg_name= "ajaytest",
            vnet_name="ajay-vnet",
            subnet_name="default",
            nic_name = "ansumanVM-nic",
            private_ip="10.0.0.10",
            vm_name="ansumanVM"
        },
        vm2={
            rg_name= "kartiktest",
            vnet_name="kartik-vnet",
            subnet_name="default",
            nic_name="terraformVM-nic",
            private_ip="10.0.0.20",
            vm_name="terraformVM"
        }
    }
}

variable "username" {
  default="ansuman"
}
variable "password" {
  default="Password@1234!"
}

data "azurerm_shared_image_version" "example" {
  name                = "0.0.1"
  image_name          = "UbuntuwithNginxinstalled"
  gallery_name        = "ansumantestgallery"
  resource_group_name = "ansumantest"
}
data "azurerm_resource_group" "rg-dock" {
    for_each = var.VM
  name = each.value["rg_name"]
}

# Fetch vm network
data "azurerm_virtual_network" "vm_network" {
    for_each = var.VM
  name                = each.value["vnet_name"]
  resource_group_name = each.value["rg_name"]
}

# Fetch vm subnet 
data "azurerm_subnet" "vm_subnet" {
    for_each = var.VM
  name                 = each.value["subnet_name"]
  resource_group_name  = each.value["rg_name"]
  virtual_network_name = each.value["vnet_name"]
}
resource "azurerm_network_interface" "ens124" {
    for_each = var.VM
  name                = each.value["nic_name"]
  location            = data.azurerm_resource_group.rg-dock[each.key].location
  resource_group_name = data.azurerm_resource_group.rg-dock[each.key].name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = data.azurerm_subnet.vm_subnet[each.key].id
    private_ip_address_allocation = "Static"
    private_ip_address            = each.value["private_ip"]
  }
}

# create and display an SSH key
resource "tls_private_key" "ssh" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

output "tls_private_key" {
  value     = tls_private_key.ssh.private_key_pem
  sensitive = true
}

resource "azurerm_linux_virtual_machine" "main" {
    for_each = var.VM
  name                            = each.value["vm_name"]
  resource_group_name             = data.azurerm_resource_group.rg-dock[each.key].name
  location                        = data.azurerm_resource_group.rg-dock[each.key].location
  size                            = "standard_ds3_v2"
  admin_username                  = var.username
  admin_password                  = var.password
  disable_password_authentication = true
  network_interface_ids           = [azurerm_network_interface.ens124[format("%s", each.key)].id]

  source_image_id = data.azurerm_shared_image_version.example.id

  computer_name = each.key

  admin_ssh_key {
    username   = var.username
    public_key = tls_private_key.ssh.public_key_openssh
  }

  os_disk {
    name                 = "disk-int-dock-${each.key}"
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }
}

Note: The admin username and the ssh username must be the same due the below limitation :

enter image description here

And the location of the VM ,Vnet ,Image and other resources must be the same.

Output:

enter image description here

enter image description here

enter image description here

  •  Tags:  
  • Related