bpg / terraform-provider-proxmox

Terraform Provider for Proxmox
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
865 stars 138 forks source link

[question] cant retrive static ip from container #1619

Open Dialgatrainer02 opened 1 week ago

Dialgatrainer02 commented 1 week ago

hello, im trying to grab the ip address of a container by using the resource and attribute eg

proxmox_virtual_environment_container.almalinux_container["adguard1"].initialization.ip_config.ipv4.address

however terraform is saying initalisation is a list of objects

│ Block type "initialization" is represented by a list of objects, so it must be indexed using a numeric key, like .initialization[0].

if i change initalization to include the index it then moves the error to ip_config and so on i have used the first method when in for each using [each.key] rather than just adguard so im confused why its not working when i hardcode the key

the rest of my code is at https://github.com/Dialgatrainer02/home-lab/tree/main/terraform this is my first time using terraform so im pretty new to it

also off topic question why does terraform always replace my vm i can run 2 apply's back to back and it will destroy and recreate my vm's

bpg commented 1 week ago

hey @Dialgatrainer02 👋🏼

that works fine:

resource "proxmox_virtual_environment_container" "test_container" {
  node_name = "pve"

  disk {
    datastore_id = "local-lvm"
    size         = 8
  }

  started  = true

  initialization {
    hostname = "test"

    user_account {
      password = "password"
    }
    ip_config {
      ipv4 {
        address = "172.17.1.1/15"
        gateway = "172.16.0.1"
      }
    }
  }

  network_interface {
    name     = "vmbr0"
    firewall = true
  }

   operating_system {
     template_file_id = "local:vztmpl/ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
     type             = "ubuntu"
   }
}

output "container-initialization" {
  value = proxmox_virtual_environment_container.test_container.initialization[0].ip_config[0].ipv4[0].address
}
OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

OpenTofu will perform the following actions:

  # proxmox_virtual_environment_container.test_container will be created
  + resource "proxmox_virtual_environment_container" "test_container" {
      + id             = (known after apply)
      + node_name      = "pve"
      + protection     = false
      + start_on_boot  = true
      + started        = true
      + template       = false
      + timeout_clone  = 1800
      + timeout_create = 1800
      + timeout_delete = 60
      + timeout_start  = 300
      + timeout_update = 1800
      + unprivileged   = false
      + vm_id          = (known after apply)

      + disk {
          + datastore_id = "local-lvm"
          + size         = 8
        }

      + initialization {
          + hostname = "test"

          + ip_config {
              + ipv4 {
                  + address = "172.17.1.1/15"
                  + gateway = "172.16.0.1"
                }
            }

          + user_account {
              + password = (sensitive value)
            }
        }

      + network_interface {
          + bridge     = "vmbr0"
          + enabled    = true
          + firewall   = true
          + mtu        = 0
          + name       = "vmbr0"
          + rate_limit = 0
          + vlan_id    = 0
        }

      + operating_system {
          + template_file_id = "local:vztmpl/ubuntu-20.04-standard_20.04-1_amd64.tar.gz"
          + type             = "ubuntu"
        }
    }

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

Changes to Outputs:
  + container-initialization = "172.17.1.1/15"
proxmox_virtual_environment_container.test_container: Creating...
proxmox_virtual_environment_container.test_container: Creation complete after 6s [id=100]

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

Outputs:

container-initialization = "172.17.1.1/15"
Screenshot 2024-11-04 at 8 52 40 PM
bpg commented 1 week ago

also off topic question why does terraform always replace my vm i can run 2 apply's back to back and it will destroy and recreate my vm's

The terraform / tofu output should provide the details why. Attributes that triggers replacement will have "forces replacement" annotations in red colour:

Screenshot 2024-07-14 at 6 39 35 PM
Dialgatrainer02 commented 1 week ago

Cool thank you for the help I'll need to hunt down the attribute forcing replacement but that was really helpful thanks

Dialgatrainer02 commented 1 week ago

I had the username as null on the VM setting it to the VM images default user fixed it and it works as expected however I'm encountering a cyclical dependency as I'm using a container to run a DNS server and I want all the vMS to use it however terraform is unhappy with me setting it to the container ip. After recourse creation can I update the DNS part of a later once they have all been made?