Working with VM Templates and DHCP in Terraform: Avoiding Duplicate IP Addresses with Ubuntu
As I continue to explore the capabilities of Terraform, I’ve encountered several challenges when working with VM templates and deploying them en masse. In particular, I’ve run into issues when trying to use DHCP for networking configuration. While Windows and CentOS gave me no problems, I faced errors when deploying an Ubuntu 18.04 template. Upon investigation, I discovered that the issue stemmed from a change in behavior in Ubuntu’s use of machine-id to request DHCP addresses. This led to all cloned VMs being assigned the same IP address. In this blog post, I’ll outline the solution I found and provide references for further reading.
The Problem: Duplicate IP Addresses with Cloned VMs
—————————————————
When working with VM templates in Terraform, I often need to create and destroy VMs frequently. For this project, I required the option to use DHCP for networking configuration. However, when deploying the Ubuntu 18.04 template, I encountered errors during plan execution. Upon examining the output of the Terraform where export the VM IP addresses, I noticed that all the cloned VMs had been assigned the same IP address.
At first, I suspected that the issue might be due to the same MAC address being assigned by ESXi to the cloned VMs, resulting in the machines being allocated the same IP. However, when I checked the MAC addresses, they were all different. After some online research, I discovered that the problem was caused by Ubuntu’s use of machine-id to request DHCP addresses.
The Cause: Ubuntu’s Use of Machine-id for DHCP Addresses
———————————————————
Ubuntu’s default networking configuration goes through cloud-init, which by default sends /etc/machine-id in the DHCP request. This leads to the duplicate IP situation. The /etc/machine-id file contains the unique machine ID of the local system that is set during installation or boot. The machine ID is a single newline-terminated, hexadecimal, 32-character, lowercase ID. When decoded from hexadecimal, this corresponds to a 16-byte/128-bit value. This ID may not be all zeros. The machine ID is usually generated from a random source during system installation or first boot and stays constant for all subsequent boots. Optionally, for stateless systems, it is generated during runtime during early boot if necessary.
The Fix: Blanking Out the Machine-id File
——————————————
From a template perspective, there is a quick fix that can be applied to avoid duplicate IP addresses with cloned VMs. The solution involves blanking out the machine-id file so that upon first boot, a new ID is generated. It’s important to note that you cannot simply delete the machine-id file as it needs to exist. If it doesn’t exist, the deployment will fail as it expects it to be there in some form.
To blank out the machine-id file, I used the following command:
“`
sudo echo “” > /etc/machine-id
“`
This will zero out the file and cause a new ID to be generated upon first boot. Once done, the VM can be saved again as a template, and the cloning operation will result in unique IPs being handed out by the DHCP server.
References
———-
For further reading, here are some references that may be helpful:
*
*
Conclusion
———-
In conclusion, when working with VM templates and DHCP in Terraform, it’s important to be aware of Ubuntu’s use of machine-id for DHCP addresses. Duplicate IP addresses can be avoided by blanking out the machine-id file so that a new ID is generated upon first boot. By following this solution, you can ensure that your cloned VMs receive unique IP addresses and avoid any potential issues with network configuration.