Setting up Ansible – Infrastructure as Code (IaC) with Terraform

We need to set up and install Ansible in the control node, but before we do that, we will have to spin three servers to start the activity—an Ansible control node and two managed nodes.

Setting up inventory

The idea is to set up a two-tier architecture with Apache and MySQL. So, let’s use Terraform to spin up the three servers.

Let’s first cd into the directory where the Terraform templates are located and then edit the terraform. tfvars file to fill in the required details. (Please refer to Chapter 8, Infrastructure as Code (IaC) with Terraform, for more details about how to get the attributes):

$ cd ~/modern-devops/ch9/setup-ansible-terraform

$ vim terraform.tfvars

Then, use the following commands to spin up the servers using Terraform:

$ terraform init

$ terraform plan -out ansible.tfplan

$ terraform apply ansible.tfplan

Once the terraform apply command is completed successfully, we will see three servers— ansible-control-node, web, and db, and the associated resources created within the ansible-exercise resource group.

The terraform apply output also provides the public IP addresses of the Ansible control node and the web VM. You should see the public IP address we got in the output.

Note

It might take a while for Azure to report the output, and if you did not get the IP addresses during terraform apply, you could subsequently run terraform output to get the details.

Ansible requires the control node to connect with managed nodes via SSH. Now, let’s move on and look at how we can communicate with our managed nodes (also known as inventory servers).

Leave a Reply

Your email address will not be published. Required fields are marked *