Hello,
I have recently upgraded to TF 0.12.19 and replaced count with for_each, as I wanted to build multiple VMs with unique configuration items. Here is part of the code that I modified to use the loop:
resource "azurerm_virtual_machine" "ubuntu" {
name = each.key
for_each = var.u_name
location = var.vm_location
resource_group_name = var.rg_vm
network_interface_ids = values(azurerm_network_interface.unic).*.id
delete_os_disk_on_termination = var.os_disk_attr["os_disk_delete"]
vm_size = each.value
u_name as defined in the variables.tf file:
variable "u_name" {
description = "Defines the VM sizes"
type = map(string)
default = {
dmp-nifi-prod-u-vm = "Standard_D2s_v3"
dmp-es-prod-u-vm = "Standard_D2s_v3"
dmp-kfk-prod-u-vm = "Standard_B2s"
dmp-utl-prod-u-vm = "Standard_B2s"
}
}
With network_interface_ids = values(azurerm_network_interface.unic).*.id, TF tries to add 4 NICs per VM. I want it to add one NIC per VM. I have referenced the terraform page that describes the use of for_each and values, but I have been unable to make it work.
I will need to use it with the local-exec provisioner as well to pick private IP addresses one at a time to run an Ansible playbook against. This is what I have in the provisioner file:
resource "null_resource" "Ansible4Ubuntu" {
for_each = var.u_name
depends_on = [
azurerm_virtual_machine.ubuntu,
azurerm_network_interface.unic,
]
#triggers = {
#network_interface_ids = values(azurerm_network_interface.unic). <em>.id
#network_interface_ids = join(",", azurerm_network_interface.unic.</em> .id)
#}
provisioner "local-exec" {
command = "sleep 5 ; ansible-playbook -i ${values(azurerm_network_interface.unic).*.id}
vmlinux-playbook.yml"
}
}
Any help would be appreciated.
Thanks