Deploying Ansible Control Nodes with Terraform on vSphere

Continuing the Spirit of Terraforming: Configuring an Ansible Control Node

In the world of IaC (Infrastructure as Code), two popular tools that have gained significant attention in recent years are Terraform and Ansible. While Terraform is used to provision and manage infrastructure, Ansible is a powerful automation tool that helps administrators configure and deploy applications efficiently. As someone who has dabbled in both, I wanted to explore the process of setting up an Ansible Control Node using Terraform. In this blog post, we’ll delve into the steps required to configure an Ansible Control Node in a repeatable and consistent manner.

Getting Started with Ansible and Terraform

Before we begin, it’s essential to have the following information ready:

1. A Terraform environment set up on your machine.

2. The CentOS Template prepared and ready for deployment from vCenter – see example configuration at

3. The Terraform templates included in this repository require Terraform to be available locally on the machine running the templates.

The setup and configuration of Ansible can be tricky, and there are a few dependencies that need to be in sync to line everything up. To make things more interesting, I had some complications working with my CentOS 7 VMware Template due to Python 2.7x being the default version that comes with that distribution build. Additionally, I included modules for Kerberos authentication when working with Windows Servers connected to Active Directory Domains. Although it wasn’t directly impacting the Playbook’s I was running, I was getting a warning while running NTLM or Kerberos authentication against any Windows server.

Given that Python 2.7 was set to be unsupported early next year, I was determined to have Ansible running off Python3. The combination and order of Linux packages and dependencies to get that working wasn’t straight forward. As you can see below in the main VM Terraform resource declaration, there are a lot of commands to make that happen.

Terraform Breakdown

The Terraform code is relatively straightforward, deploying a cloned CentOS 7 Virtual Machine with the configured network setup via the Terraform Guest Customizations. Once the VM has been deployed and configured, the initial package deployment takes place…there are then two separate configuration scripts which are uploaded and executed via SSH via the remote-exec blocks. The last remote-exec block is the list of commands that work to install Ansible with PIP and use Python3. The final command of the Terraform Plan execution is to list the installed Ansible version.

End to end, the deployment takes about 7-8 minutes depending on your storage… Once done, we have a fully functional Ansible Control Node ready for automation goodness! This might seem like a little chicken or the egg… but Terraform and Ansible represent both sides of the IaC spectrum. As I mention in the README.md, it’s time to try working out this Ansible thing a little more!

Conclusion

In conclusion, setting up an Ansible Control Node using Terraform requires some careful planning and execution. While there were some complications along the way, the end result is well worth the effort. With this setup, you have a powerful IaC tool that can automate your infrastructure and application deployments efficiently. As you continue to explore the world of Ansible and Terraform, remember that these tools represent both sides of the IaC spectrum. So, it’s time to try working out this Ansible thing a little more!