dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.54k stars 457 forks source link

Lost ip address of virtual machine from "terraform show" after start vm #850

Open unix196 opened 3 years ago

unix196 commented 3 years ago
terraform version
Terraform v0.15.0
on linux_amd64
+ provider registry.terraform.io/dmacvicar/libvirt v0.6.3

Your version of Terraform is out of date! The latest version
is 0.15.3. You can update by downloading from https://www.terraform.io/downloads.html
variables.tf:
variable "vm_network_addresses" {
  description = "Defines the network in the CIDR format"
  default = "10.0.1.0/24"
}

variable "vm_network_name" {
  description = "Network name for vm network"
  default = "vm_network"
}
libvirt.tf:

resource "libvirt_network" "vm_network" {
  name      = var.vm_network_name
  addresses = [var.vm_network_addresses]
  autostart = true
  mode      = "nat"

  dhcp {
    enabled = true
  }

  dns {
    local_only = true
  }
}
....
resource "libvirt_domain" "vm1" {
  name   = "vm1"
  memory = "336"
  vcpu   = 1

  network_interface {
    network_id     = libvirt_network.vm_network.id
    wait_for_lease = true
  }

output "ips_vm1" {
  value = libvirt_domain.vm1.network_interface.*.addresses
}
...

Run terraform apply, virtual machine is start.

terraform output

ips_vm1 = tolist([
  tolist([
    "10.0.1.228",
  ]),
])
ips_vm2 = tolist([
  tolist([
    "10.0.1.67",
  ]),
])

Then I stop vm (manually), then run terraform commands:

terraform plan
...
Terraform will perform the following actions:

  # libvirt_domain.vm1 will be updated in-place
  ~ resource "libvirt_domain" "vm1" {
        id          = "02e4f66f-a5bd-437d-bf6e-02b19cbfaff4"
        name        = "vm1"
      ~ running     = false -> true
        # (10 unchanged attributes hidden)

        # (4 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Changes to Outputs:
  ~ ips_vm1 = [
      - [
          - "10.0.1.228",
        ],
        [],
      + [],
    ]

terraform apply -auto-approve
...

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Outputs:

ips_vm1 = tolist([
  tolist([]),
])
ips_vm2 = tolist([
  tolist([
    "10.0.1.67",
  ]),
])

In terraform show also didn't show ip address vm. Is it bug or feature? (After start vm from terraform apply vm receive the same address )