I want to create 5 machines (3 frontend, 1 LoadBalncer and 1 db). I want to ensure that all VMs have /etc/hosts entries pointing to each other with the following entries:
vm1 10.0.x.x
vm2 10.0.x.x
vm3 10.0.x.x
vm4 10.0.x.x
vm5 10.0.x.x
And when IPs or hostnames changed, rerunning the terraform should update the /etc/hosts.
Below is snippet of my terraform code:
resource "azurerm_linux_virtual_machine" "frontend_vm" {
count = 3
name = "frontend-vm${count.index + 1}"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
size = "Standard_B1s"
admin_username = "adminuser"
network_interface_ids = [azurerm_network_interface.frontend_nic[count.index].id]
admin_password = "Test@1234"
disable_password_authentication = false
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
resource "azurerm_linux_virtual_machine" "loadbalancer_vm" {
name = "loadbalancer-vm"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
size = "Standard_B1s"
admin_username = "adminuser"
network_interface_ids = [azurerm_network_interface.loadbalancer_nic.id]
admin_password = "Test@1234"
disable_password_authentication = false
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
resource "azurerm_linux_virtual_machine" "database_vm" {
name = "database-vm"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
size = "Standard_B1s"
admin_username = "adminuser"
network_interface_ids = [azurerm_network_interface.database_nic.id]
admin_password = "Test@1234"
disable_password_authentication = false
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
May be we can use remote-exec but I’m not familiar with that. Any help will be highly appreciated.