A
u
Belaid Youba Hadj Arab, Etienne Deneuve
Oct 4, 2023 · 4 min read

Deploying a virtual machine with Terraform and Ansible

Side view worker wearing gloves

Infrastructure deployment is a common practice in the Cloud world. This can be done either manually or automatically. In this article, we’re going to focus on the second approach. To do this, we’ll need Terraform, a powerful tool, as well as Ansible for the infrastructure configuration.

What is Terraform ?

  • Terraform is a BSL-licensed tool used to automate the deployment and management of cloud infrastructures.
  • It allows you to describe the desired infrastructure in configuration files, then execute them to create and manage the necessary resources.
  • It uses HCL (HashiCorp Configuration Language, unlike hydrogen chloride, which is a gas :D ).
  • This is a simple, human-readable language designed to describe resources and configurations declaratively.

What is Ansible ?

Ansible is an open source software package which, like Terraform, enables infrastructure deployment, as well as application management and configuration. It uses YAML (Yet Another Markup Language) to describe the tasks to be performed, and playbooks to automate operations.

Prerequisites

For our little adventure, we’ll need certain prerequisites:

  1. An Azure subscription
  2. Azure CLI
  3. Terraform
  4. Ansible
  5. Homebrew(MacOS)
  6. SSH key

Installation

1. Terraform

Here, I’m going to concentrate on an installation with manager packages (such as Homebrew for Mac, and Chocolatey for Windows) as I find this easier.

Terraform on macOS

  1. Install the HashiCorp repository for Homebrew :
Terminal window
brew tap hashicorp/tap
  1. Then install Terraform:
Terminal window
brew install hashicorp/tap/terraform
  1. To check that the installation has gone smoothly, run the following command on a terminal:
Terminal window
terraform --version
# résultat
Terraform v1.4.6

Terraform on Linux(Ubuntu)

  1. Update the system and install some components:
Terminal window
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
  1. Install HashiCorp GPG Key:
Terminal window
wget -O- <https://apt.releases.hashicorp.com/gpg> | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
  1. Check the GPG Key :
Terminal window
gpg --no-default-keyring
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg
--fingerprint
  1. Add the HashiCorp repository to the system:
Terminal window
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
<https://apt.releases.hashicorp.com> $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
  1. Finally, install terraform:
Terminal window
sudo apt update && apt install terraform

For other types of installation, take a look at the Terraform documentation.

2. Ansible

Ansible on macOS

  1. Generate an SSH key, which will come in handy later:
Terminal window
ssh-keygen
  1. simply issue the command :
Terminal window
brew install ansible
  1. Check installation:
Terminal window
ansible -version
# résultat
ansible [core 2.13.1]

Ansible on Linux (Ubuntu)

  1. Generate SSH key :
Terminal window
ssh-keygen
  1. Install Ansible :
Terminal window
sudo apt update && apt install ansible
  1. Same verification as for MacOS:
Terminal window
ansible -version
# résultat
ansible [core 2.13.1]

For more information, read the Ansible documentation.

Ansible on Windows

Unfortunately, there’s no Ansible on Windows, so the best solution is either to use a linux virtual machine, or the one I prefer, using WSL 2. To remain consistent, we’ll also install Terraform with WSL :

  1. Open a prompt command as administrator, and run the following command:
Terminal window
wsl.exe -install
  1. Once installed, and after rebooting the system, you can launch the Ubuntu app from the Start menu.
  2. You’re now in front of an Ubuntu terminal, so all you need to do is follow the same steps outlined above for installing Terraform and Ansible for Ubuntu.
Ansible Terraform Azure

Related articles