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

Static IP for libvirt_domain network_interface #951

Open awolffredhat opened 2 years ago

awolffredhat commented 2 years ago

System Information

Linux distribution

RHEL

Description of Issue/Question

libvirt_domain network_interface has an optional addresses field. When using this field it has created a dynamic ip address with the value I provided, and created a dhcp entry for this ip address. Is it possible to create a static IP address, with no dhcp entry, for the network_interface? Thanks in advance.

drdev commented 2 years ago

There is no way the KVM host could force a VM with unknown OS and unknown config to use a certain network configuration. Most likely you'll need to create an appropriate cloud-init config to manage IP address of VM, if your OS is cloud-init enabled.

awolffredhat commented 2 years ago

Thank you.

rabin-io commented 1 year ago

Another option is to hardcode the MAC of the VM, and set a reservation in the network config. No static, but it is fixed.

<network>
  <name>default</name>
  <uuid>e346291e-f86b-4f2f-a16e-654136441805</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:12:fe:35'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.100' end='192.168.122.254'/>
      <host mac='52:54:00:a0:cc:19' name='centos7' ip='192.168.122.2'/>
      <host mac='52:54:00:f7:a1:c8' name='puffy' ip='192.168.122.3'/>
      <host mac='52:54:00:4c:40:1c' name='xenial' ip='192.168.122.4'/>
    </dhcp>
  </ip>
</network>
awolffredhat commented 1 year ago

Thanks! I was specifically looking to use this on a network with no dhcp server. I implemented a solution that created the ip address using nmcli commands after the server is running.

rabin-io commented 1 year ago

The DHCP I was referring to is internal to libvirt, and can be used when you set the interface mode to NAT. But that is not the case if you bridge your interface to a local nic. But I had to resolve to the same method as you, with nmcli commands to achieve the same result.

awolffredhat commented 1 year ago

OK. Thanks anyway :)