Deploying a virtual machine with Terraform and Ansible
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:
- An Azure subscription
- Azure CLI
- Terraform
- Ansible
- Homebrew(MacOS)
- 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
- Install the HashiCorp repository for Homebrew :
brew tap hashicorp/tap- Then install Terraform:
brew install hashicorp/tap/terraform- To check that the installation has gone smoothly, run the following command on a terminal:
terraform --version # résultat Terraform v1.4.6Terraform on Linux(Ubuntu)
- Update the system and install some components:
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common- Install HashiCorp GPG Key:
wget -O- <https://apt.releases.hashicorp.com/gpg> | \ gpg --dearmor | \ sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg- Check the GPG Key :
gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint- Add the HashiCorp repository to the system:
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- Finally, install terraform:
sudo apt update && apt install terraformFor other types of installation, take a look at the Terraform documentation.
2. Ansible
Ansible on macOS
- Generate an SSH key, which will come in handy later:
ssh-keygen- simply issue the command :
brew install ansible- Check installation:
ansible -version# résultatansible [core 2.13.1]Ansible on Linux (Ubuntu)
- Generate SSH key :
ssh-keygen- Install Ansible :
sudo apt update && apt install ansible- Same verification as for MacOS:
ansible -version# résultatansible [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 :
- Open a prompt command as administrator, and run the following command:
wsl.exe -install- Once installed, and after rebooting the system, you can launch the
Ubuntuapp from theStartmenu. - 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.