What's the best way to create a droplet with block storage using Terraform and Ansible?

September 5, 2019 67 views
Ansible Terraform Block Storage Ubuntu 18.04

Hi

I’m in the process of automating my infrastructure on DO.

I have all droplets and volumes created using terraform, and I’m also using user data during the provisioning for things like defining base packages, creating users and some other services like ssh.

I also have ansible playbooks for all droplets that install and configure the services, like mongo, nginx, etc…

Now I’m faced with a decision that I don’t know how to make.

I want to mount a volume to a droplet. Terrafrom creates and assigns the volume but does not mount it.

So I can either use the user data option and try to script that mount point or include this in the ansible playbook.

Can anyone tell me the advantages and disadvantages of both approaches? Or if there’s another one that I did not consider?

For me this is infrastructure, so terraform makes more sense, but doing conditionals (like only format if no partition) on user data does not seem right, and I don’t want to use provisioners in terraform (sinse that’s ansible’s role).

Thanks in advance for any insight that you can give me

1 Answer
asb MOD September 5, 2019
Accepted Answer

Your approach to using Terraform for provisioning infrastructure and Ansible for config management is the right way to think about it. Luckily Terraform should be able to do what you’d like. Pre-formatted volumes created via the API will be automatically mounted to the Droplet. So the key thing is to specify the initial_filesystem_type when creating the volume with Terraform. For example, using this config:

resource "digitalocean_droplet" "foo" {
  name       = "foo"
  size       = "s-1vcpu-1gb"
  image      = "ubuntu-18-04-x64"
  region     = "nyc1"
}

resource "digitalocean_volume" "bar" {
  region                  = "nyc1"
  name                    = "bar"
  size                    = 100
  initial_filesystem_type = "ext4"
  description             = "an example volume"
}

resource "digitalocean_volume_attachment" "foobar" {
  droplet_id = "${digitalocean_droplet.foo.id}"
  volume_id  = "${digitalocean_volume.bar.id}"
}

The resulting volume will be mounted and available at /mnt/bar/ on the Droplet.

If you do not specify the initial_filesystem_type the volume will appear as a raw block device that must be manually formatted and mounted.

Have another answer? Share your knowledge.