Terraform-provider-libvirt: No IP address is assigned with Ubuntu example?

Created on 1 Aug 2019  路  10Comments  路  Source: dmacvicar/terraform-provider-libvirt

System Information

Not sure if it's related to https://github.com/dmacvicar/terraform-provider-libvirt/issues/312

Full log of TF_LOG=debug terraform apply here: https://gist.github.com/chrissound/d30e2a6a7863d53deeacc63a6a588fe5

$ terraform refresh
libvirt_volume.ubuntu-qcow3: Refreshing state... (ID: /var/lib/libvirt/images/ubuntu-qcow3)
data.template_file.user_data: Refreshing state...
data.template_file.network_config: Refreshing state...
libvirt_cloudinit_disk.commoninit2: Refreshing state... (ID: /var/lib/libvirt/images/commoninit.iso;5d42cf95-cde7-b95b-e493-6b040c7c2d5b)
libvirt_domain.domain-ubuntu: Refreshing state... (ID: 76a842bf-2396-4b98-8270-964aa6ddb953)
$ terraform show
data.template_file.network_config:
  id = df16c3f9c2a6d8255670be81539ec54a42d2d59eda005c29c22efb1dd9e28909
  rendered = network:
  version: 2
  config:
  - type: physical
    name: ens3
    subnets:
      - type: dhcp

  template = network:
  version: 2
  config:
  - type: physical
    name: ens3
    subnets:
      - type: dhcp

data.template_file.user_data:
  id = 3a35a94c06c5e51de4515a34202bafc7d600081e185a589d855ca95abab5d175
  rendered = #cloud-config
# vim: syntax=yaml
#
# ***********************
#   ---- for more examples look at: ------
# ---> https://cloudinit.readthedocs.io/en/latest/topics/examples.html
# ******************************
#
ssh_pwauth: True
chpasswd:
  list: |
     root:1234
  expire: False

  template = #cloud-config
# vim: syntax=yaml
#
# ***********************
#   ---- for more examples look at: ------
# ---> https://cloudinit.readthedocs.io/en/latest/topics/examples.html
# ******************************
#
ssh_pwauth: True
chpasswd:
  list: |
     root:1234
  expire: False

libvirt_cloudinit_disk.commoninit2:
  id = /var/lib/libvirt/images/commoninit.iso;5d42cf95-cde7-b95b-e493-6b040c7c2d5b
  meta_data = 
  name = commoninit.iso
  network_config = network:
  version: 2
  config:
  - type: physical
    name: ens3
    subnets:
      - type: dhcp

  pool = default
  user_data = #cloud-config
# vim: syntax=yaml
#
# ***********************
#   ---- for more examples look at: ------
# ---> https://cloudinit.readthedocs.io/en/latest/topics/examples.html
# ******************************
#
ssh_pwauth: True
chpasswd:
  list: |
     root:1234
  expire: False

libvirt_domain.domain-ubuntu:
  id = 76a842bf-2396-4b98-8270-964aa6ddb953
  arch = x86_64
  autostart = false
  cloudinit = /var/lib/libvirt/images/commoninit.iso;5d42cf95-cde7-b95b-e493-6b040c7c2d5b
  cmdline.# = 0
  console.# = 2
  console.0.source_path = 
  console.0.target_port = 0
  console.0.target_type = serial
  console.0.type = pty
  console.1.source_path = 
  console.1.target_port = 1
  console.1.target_type = virtio
  console.1.type = pty
  disk.# = 1
  disk.0.file = 
  disk.0.scsi = false
  disk.0.url = 
  disk.0.volume_id = /var/lib/libvirt/images/ubuntu-qcow3
  disk.0.wwn = 
  emulator = /run/libvirt/nix-emulators/qemu-system-x86_64
  firmware = 
  graphics.# = 1
  graphics.0.autoport = true
  graphics.0.listen_type = address
  graphics.0.type = spice
  initrd = 
  kernel = 
  machine = pc
  memory = 800
  name = ubuntu-terraform
  network_interface.# = 1
  network_interface.0.addresses.# = 0
  network_interface.0.bridge = 
  network_interface.0.hostname = 
  network_interface.0.mac = EE:05:09:A1:19:C3
  network_interface.0.macvtap = 
  network_interface.0.network_id = e2f1a2c0-268e-47bc-bbfd-5558cdc6cd2d
  network_interface.0.network_name = default
  network_interface.0.passthrough = 
  network_interface.0.vepa = 
  network_interface.0.wait_for_lease = false
  nvram.# = 0
  qemu_agent = false
  running = true
  vcpu = 1
libvirt_volume.ubuntu-qcow3:
  id = /var/lib/libvirt/images/ubuntu-qcow3
  format = qcow2
  name = ubuntu-qcow3
  pool = default
  size = 2361393152
  source = /home/chris/NewProjects/tfTest3Mysql/ubuntu-16.04-server-cloudimg-amd64-disk1.img

Linux distribution

Nixos 19.03

Terraform version

terraform --version
Terraform v0.11.14

Provider and libvirt versions

terraform-provider-libvirt -version 
zsh: command not found: terraform-provider-libvirt

Checklist

Description of Issue/Question

Setup

(Please provide the full _main.tf_ file for reproducing the issue (Be sure to remove sensitive information)

ubuntu.tf (I got this from the examples directory - possibly it's not the latest):

provider "libvirt" {
  uri = "qemu:///system"
}

resource "libvirt_volume" "ubuntu-qcow3" {
  name   = "ubuntu-qcow3"
  pool   = "default"
  source = "/home/chris/NewProjects/tfTest3Mysql/ubuntu-16.04-server-cloudimg-amd64-disk1.img"
  format = "qcow2"
}

data "template_file" "user_data" {
  template = "${file("${path.module}/cloud_init.cfg")}"
}

data "template_file" "network_config" {
  template = "${file("${path.module}/network_config.cfg")}"
}

resource "libvirt_cloudinit_disk" "commoninit2" {
  name           = "commoninit.iso"
  user_data      = "${data.template_file.user_data.rendered}"
  network_config = "${data.template_file.network_config.rendered}"
}

resource "libvirt_domain" "domain-ubuntu" {
  name   = "ubuntu-terraform"
  memory = "800"
  vcpu   = 1

  cloudinit = "${libvirt_cloudinit_disk.commoninit2.id}"

  network_interface {
    network_name = "default"
  }

  # IMPORTANT: this is a known bug on cloud images, since they expect a console
  # we need to pass it
  # https://bugs.launchpad.net/cloud-images/+bug/1573095
  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  disk {
    volume_id = "${libvirt_volume.ubuntu-qcow3.id}"
  }

  graphics {
    type        = "spice"
    listen_type = "address"
    autoport    = true
  }
}

# IPs: use wait_for_lease true or after creation use terraform refresh and terraform show for the ips of domain

need info question

Most helpful comment

@MalloZup I don't think i touched anything that will affect the networking bits.

All 10 comments

Hi @chrissound thx for issue. At first glance I don't get the issue but I'm checking the issue with mobile so we need to check more ln detail

Sure thanks. Basically I'm expecting network_interface.0.addresses.# = 0 to have an IP address listed there instead of 0.

@chrissound do you have a network iface called default? afaik back in the time this worked @zeenix CC as he updated last time. ( I also remember that I used multiple times that examples without any pb)

Also try after the terraform apply, the commands terraform refresh and terraform show,

The IP behaviour is explained here https://github.com/dmacvicar/terraform-provider-libvirt/blob/19c23424ccfdecf6dfcbdd87783bf835ba5f585e/website/docs/r/domain.html.markdown#L344

Nomally we don't wait for IP , so I think it is normall that you don't have it. You need either to do terraform refresh, and terraform show

Will have to investigate this again, thanks. But just thought I'd quickly reply and say I did run the refresh and show commands after the apply (it's included in the post - first code snippet).

@MalloZup I don't think i touched anything that will affect the networking bits.

@chrissound ok I will retry on my side to check. thx for feedback both of you !

@chrissound @MalloZup

I tried this terraform provider yesterday and had the same issue. After some digging, the below config works for me in both Ubuntu 16.04 and 18.04.

Working network_config.cfg (see attached file for correct indentation)
version: 2
ethernets:
ens3:
dhcp4: true

Description of changes
In detail, two changes are required.

  1. Drop the initial "network:" in the file
  2. Update the syntax to that found in the cloudinit documentation here
    https://cloudinit.readthedocs.io/en/latest/topics/network-config-format-v2.html#network-config-v2
    (I couldn't pinpoint the exact date the format changed)

Ubuntu examples updated in #632

The Suse / Leap15 examples may also be affected.

I will have a look at these later.

network_config.cfg.txt

@chrissound

Can you please confirm if this fixes the underlying issues with retrieving the ip per your original post.

I didn't try your exact configuration, but the above fixed my issue of not seeing the ens3 interface when logged into the vm.

thx for PR. If it fix issue we can merge. :rose:

Yup that fixes it. Many thanks to all involved!

Was this page helpful?
0 / 5 - 0 ratings