Latest update


Terraform prompt for variable input when creating a virtual machine

Terraform is a great infrastructure as code scripting language from Hashicorp. I use it day in and day out in the lab and in production environments to streamline the process of deploying infrastructure. In the lab environment, I have had a Terraform script that allows cloning and customizing a Linux virtual machine template I have running in VMware vSphere. However, I have been editing the file manually to specify certain parameters, such as the virtual machine name, the vSphere inventory name, IP address, etc. I wanted to update this to instead prompt for these values so I can enter these on the fly. Let’s look at Terraform prompt for variable during virtual machine creation and see how we can do this.

What are Terraform variables?

Like any other programming or scripting language, Terraform enables the ability to use variables in your code. Variables make working with code and various scripts much easier as they provide a placeholder allowing for automated input when required or for prompting for input as we want to accomplish here.

There are two files in the Terraform HCL (Hashicorp Configuration Language) that allow you to define variables. These are the:

  • – The file allows you to define the variables
  • terraform.tfvars – The terraform.tfvars file is the file that is generally used to define values for the variables you have created in the file.


Terraform prompt for variable input when creating a virtual machine

So, in the first version of my Terraform code, I have something similar to the following defined in the files:

variable "ipv4" {}


ipv4 = ""

However, this means, that each time I want to clone a virtual machine template to customize the Linux virtual machine, I have to manually go in and change the IP address defined. The same is true for the Linux hostname and the vSphere inventory name.

The solution is quite simple. Instead of defining the value in the terraform.tfvars file, you can simply leave it out and only define the variable itself in the file. When you do this, if the variable is indeed required information, which IP address, and others are, it will instead prompt you for the value you want to use.

You can see how I reference the variables in the code below, which is a block of my file. As you can see below, I have a couple of variables that I am prompting for, var.linux_hostname and the var.ipv4, which as mentioned, are both defined only in the file and not the terraform.tfvars file.

clone {
    template_uuid = "${}"

    customize {
      linux_options {
        host_name = var.linux_hostname
        domain    = "cloud.local"

      network_interface {
        ipv4_address = var.ipv4
        ipv4_netmask = 24

      ipv4_gateway = ""

It results in the following behavior when running a terraform plan and terraform apply.

Terraform prompt for variable input during a virtual machine clone operation
Terraform prompt for variable input during a virtual machine clone operation

See my full implementation of how to clone a Linux virtual machine using Terraform.

Terraform prompt for input variable FAQs

  • What is Terraform? Terraform is arguably the leading infrastructure as code offering allowing DevOps engineers to define infrastructure as code and operate in a declarative manner. It provides hundreds of modules allowing you to work with a wide range of “providers” which are the environments that Terraform can “speak” to.
  • What is infrastructure as code? It is a new way of defining infrastructure in code. It has been brought about by the introduction of cloud infrastructure that can be controlled using API endpoints.
  • What are variables? Variables are ways of defining placeholder values that allow automatically inserting values or prompting for input from a user.

Wrapping Up

If you haven’t got your hands on Terraform, you need to. It is a great way to interact with your infrastructure, including on-premises infrastructure like VMware vSphere. It allows you to define infrastructure in a declarative way. Hopefully, this little tutorial on how to configure Terraform prompt for variable behavior will help any who want to implement this in their terraform scripts.

Post a Comment