Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] cant retrive static ip from container #1619

Open
Dialgatrainer02 opened this issue Nov 4, 2024 · 4 comments
Open

[question] cant retrive static ip from container #1619

Dialgatrainer02 opened this issue Nov 4, 2024 · 4 comments
Labels
❓ question Further information is requested

Comments

@Dialgatrainer02
Copy link

Dialgatrainer02 commented Nov 4, 2024

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

@Dialgatrainer02 Dialgatrainer02 changed the title [question] cant rertive static ip from container [question] cant retrive static ip from container Nov 4, 2024
@bpg
Copy link
Owner

bpg commented Nov 5, 2024

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 bpg added the ❓ question Further information is requested label Nov 5, 2024
@bpg
Copy link
Owner

bpg commented Nov 5, 2024

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
Copy link
Author

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

@Dialgatrainer02
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants