oVirt / terraform-provider-ovirt

Terraform provider for oVirt 4.x
https://registry.terraform.io/providers/oVirt/ovirt/latest/docs
Other
137 stars 72 forks source link

HELP use ovirt_wait_for_ip (Data Source) #489

Closed PDiogoVS closed 1 year ago

PDiogoVS commented 1 year ago

First, let me report that I try to use ovirt_wait_for_ip as described in the documentation, but it's not working.

Documentation: ... // Expect an IP address to be reported by the VM resource "ovirt_wait_for_ip" "test" { vm_id = ovirt_vm_start.test.vm_id ...

Correction ... // Expect an IP address to be reported by the VM data "ovirt_wait_for_ip" "test" { vm_id = ovirt_vm_start.test.vm_id ...

But my problem and request for help is... I try to use this data to get an IP of a vm and make a connection on another resource, so my data is: ...

test = {
  "id" = "ba67d183-b7cf-4fc3-bb64-f49ae62a0e58"
  "interfaces" = toset([
    {
      "ipv4_addresses" = toset([
        "192.168.101.214",
      ])
      "ipv6_addresses" = toset([])
      "name" = "enp1s0"
    },
  ])
  "vm_id" = "ba67d183-b7cf-4fc3-bb64-f49ae62a0e58"
}

...

how can i use or put this data in a variable like this: ip="192.168.101.214"

my next attempt is:

final_ip = toset([
  "192.168.101.214",

with code:


locations {
  ips = element(tolist(data.ovirt_wait_for_ip.test["tf2.dc.ubiwhere.lan"].interfaces), 0)
  final_ip = local.ips.ipv4_addresses
}
engelmi commented 1 year ago

Hi @PDiogoVS, thanks for your report! I updated the examples and documentation for the datasource wait-for-ip. The example here shows how to access the ipv4. Does this answer your question?

PDiogoVS commented 1 year ago

close, but not responding :)

with your example i have the following output:

test = tolist([
  toset([
    "192.168.101.212",
  ]),
])

but when i try to use data.ovirt_wait_for_ip.test["test"].interfaces.*.ipv4_addresses to do a connection like:

resource "null_resource" "example1" {
  connection {
    host     = data.ovirt_wait_for_ip.test["test"].interfaces.*.ipv4_addresses
    user     = "root"
    password = var.ovirt_password
    agent   = false
    timeout = "3m"
  }

  provisioner "remote-exec" {
    # Leave this here so we know when to start with Ansible local-exec 
    inline = [
      "echo 'Cool, we are ready for provisioning'",
      "touch /tmp/terraform_hello.demo"
    ]
  }
  depends_on = [data.ovirt_wait_for_ip.test]
}

I have following error:

Error: Incorrect attribute value type
│ 
│   on main.tf line 80, in resource "null_resource" "example1":
│   80:     host = data.ovirt_wait_for_ip.test["test"].interfaces.*.ipv4_addresses
│     ├────────────────
│     │ data.ovirt_wait_for_ip.test["test"].interfaces is set of object with 1 element
│ 
│ Inappropriate value for attribute "host": string required.

i think output you send it's a list or map you know how i can use only the first item?

engelmi commented 1 year ago

Please refer to this https://github.com/oVirt/terraform-provider-ovirt/issues/474#issuecomment-1235743427

As stated there:

The field ipv4_addresses on the ovirt_wait_for_ip datasource is a nested set of strings, which is quite cumbersome to deal with in terraform.

Does the linked comment solve your problem?

PDiogoVS commented 1 year ago

Yes!!! Thank you