hitcounter
  • April 22, 2018
  • Yuriy Medvedev

Terraform as Continuous delivery(CD) tool for automation virualbox

As you know Hashicorp provide many providers for Terraform, few month ago I tried to found provider for virtualbox, on github I found experimental provider and begun to maintain it.

Nowadays Virtualbox terraform provider has basic functionality and you can use it for poc or for developers

How to install VirtualBox terraform provider?

The installation so simple, for installation you can use command


go get github.com/pyToshka/terraform-provider-virtualbox
mkdir ~/.terraform.d/plugins
cp $GOPATH/bin/terraform-provider-virtualbox  ~/.terraform.d/plugins

How to use VirtualBox terraform provider?

Now we should create new terraform project

mkdir virtualbox-example 
cd virtualbox-example
touch main.tf && vi main.tf

Exemple of main.tf

resource "virtualbox_vm" "node" {
    count = 2
    name = "${format("node-%02d", count.index+1)}"
    url = "https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20180420.0.0/providers/virtualbox.box"
    image = "./virtualbox.box"
    cpus = 2
    memory = "512 mib",
   // user_data = "${file("user_data")}"


     network_adapter {
       type = "bridged",
       host_interface="en0"
    }


}


Now we should initialize terraform provider

terraform init

As result, you should get output something like below

terraform init

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Ok, now we ready to use provider, get starting

terraform plan

Basic output should be

terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + virtualbox_vm.node[0]
      id:                                       <computed>
      cpus:                                     "2"
      image:                                    "./virtualbox.box"
      memory:                                   "512 mib"
      name:                                     "node-01"
      network_adapter.#:                        "1"
      network_adapter.0.device:                 "IntelPro1000MTServer"
      network_adapter.0.host_interface:         "en0"
      network_adapter.0.ipv4_address:           <computed>
      network_adapter.0.ipv4_address_available: <computed>
      network_adapter.0.mac_address:            <computed>
      network_adapter.0.status:                 <computed>
      network_adapter.0.type:                   "bridged"
      status:                                   "running"
      url:                                      "https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20180420.0.0/providers/virtualbox.box"

  + virtualbox_vm.node[1]
      id:                                       <computed>
      cpus:                                     "2"
      image:                                    "./virtualbox.box"
      memory:                                   "512 mib"
      name:                                     "node-02"
      network_adapter.#:                        "1"
      network_adapter.0.device:                 "IntelPro1000MTServer"
      network_adapter.0.host_interface:         "en0"
      network_adapter.0.ipv4_address:           <computed>
      network_adapter.0.ipv4_address_available: <computed>
      network_adapter.0.mac_address:            <computed>
      network_adapter.0.status:                 <computed>
      network_adapter.0.type:                   "bridged"
      status:                                   "running"
      url:                                      "https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20180420.0.0/providers/virtualbox.box"


Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

After it, we can start to deploy our infrastructure

 terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + virtualbox_vm.node[0]
      id:                                       <computed>
      cpus:                                     "2"
      image:                                    "./virtualbox.box"
      memory:                                   "512 mib"
      name:                                     "node-01"
      network_adapter.#:                        "1"
      network_adapter.0.device:                 "IntelPro1000MTServer"
      network_adapter.0.host_interface:         "en0"
      network_adapter.0.ipv4_address:           <computed>
      network_adapter.0.ipv4_address_available: <computed>
      network_adapter.0.mac_address:            <computed>
      network_adapter.0.status:                 <computed>
      network_adapter.0.type:                   "bridged"
      status:                                   "running"
      url:                                      "https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20180420.0.0/providers/virtualbox.box"

  + virtualbox_vm.node[1]
      id:                                       <computed>
      cpus:                                     "2"
      image:                                    "./virtualbox.box"
      memory:                                   "512 mib"
      name:                                     "node-02"
      network_adapter.#:                        "1"
      network_adapter.0.device:                 "IntelPro1000MTServer"
      network_adapter.0.host_interface:         "en0"
      network_adapter.0.ipv4_address:           <computed>
      network_adapter.0.ipv4_address_available: <computed>
      network_adapter.0.mac_address:            <computed>
      network_adapter.0.status:                 <computed>
      network_adapter.0.type:                   "bridged"
      status:                                   "running"
      url:                                      "https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20180420.0.0/providers/virtualbox.box"


Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


The Virtual machine has been created, and you can start to use new virtual machine has created via Terraform from Hashicorp. In my opinion terraform the best tool for CI and CD automation.

Other features, you can use user data, example of the user data:

{
    "role": "worker",
    "foo": "bar"
}