Hound
Hound

Reputation: 927

How to access private ip in terraform azure local-exec provisioner?

I have the below terrafrom code to create a VM in azure and I want to execute a command through local-exec provisioner

resource "azurerm_resource_group" "resource_gp" {
    name     =  var.azurerm_resource_group
    location =  var.azurerm_location
}

resource "azurerm_virtual_machine" "vm" {
  count                   = var.azure_vm_count
  name                    = var.azurerm_vm_name
  location                = azurerm_resource_group.resource_gp.location
  resource_group_name     = azurerm_resource_group.resource_gp.name
  network_interface_ids   = [azurerm_network_interface.nic.id]
  vm_size                 = var.azurerm_vm_size

  storage_os_disk {
    name              = "${var.azurerm_vm_name}-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = var.azurerm_vm_managed_disk_type
    disk_size_gb      = var.azurerm_vm_disk_size
  }

  storage_data_disk {
    name              = "${var.azurerm_vm_name}-datadisk"
    create_option     = "Empty"
    caching           = "ReadWrite"
    lun               = 1
    disk_size_gb      = var.azurerm_vm_disk_size
  }

  storage_image_reference {
    id = var.azurerm_image_id
  }

  os_profile {
    computer_name  = var.azurerm_vm_name
    admin_username = var.azurerm_vm_username
    admin_password = var.azurerm_vm_password
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  provisioner "local-exec" {
    command = "echo ${self.private_ip} >> /tmp/privateip" 
  }  

}
resource "azurerm_network_interface" "nic" {
  name                = "${var.azurerm_vm_name}-nic"
  location            = azurerm_resource_group.resource_gp.location
  resource_group_name = azurerm_resource_group.resource_gp.name

  ip_configuration {
    name                            = "${var.azurerm_vm_name}-nic-config"
    subnet_id                       = var.azurerm_vnet_subnet_id
    private_ip_address_allocation   = "dynamic"
  }
}

I get the below error while executing the code

Error: Unsupported attribute

  on main.tf line 69, in resource "azurerm_network_interface" "nic":
  69:     command = "echo ${self.private_ip} >> /tmp/privateip"

This object has no argument, nested block, or exported attribute named
"private_ip".

The commands I am trying are

terraform init
terraform plan -out terraform.state
terraform apply terraform.state

Does anyone know what I am doing wrong here? How to access private ip inside local-exec provisioner?

Upvotes: 0

Views: 3539

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

In case you just need to get private Ip of the NIC - you need to access NIC properties:

azurerm_network_interface.nic.private_ip_address

Upvotes: 1

Related Questions