This article highlights the difference between Terraform and Ansible, also explaining the terraform environment.
Terraform and Ansible
It is often a topic of discussion about whether one should use Terraform or Ansible for infrastructure management.
They are both infrastructure as codes. Aiding automation with infrastructures.
• It’s core function is configuration management
• It configures infrastructures that are provisioned.
• Deploys app, installs/updates software.
• Its core function is infrastructure provisioning
• It is more advanced in orchestration.
• It’s better at provisioning infrastructure than configuration management
• They can both perform each other’s core functions.
• Open source
• Agentless; no need to deploy agents on machines.
Terraform works best for orchestration while Ansible is great at configuration management.
They have the capabilities to perform each other’s tasks, although in a limited manner.
The Terraform environment
Provider: a provider is a terraform plugin that allows users to manage an external API. Terraform works with lots of plugins, and a popular one is AWS, with Provider you’re able to set up infrastructures in AWS. They give you access to the entire API.
Resources: they describe one or more infrastructure objects, such as virtual networks, compute instances; or higher-level components such as DNS records.
Data sources: this allows data to be fetched for use in Terraform configuration, they query your existing data and use the information gotten to set up your Terraform infrastructure.
Variables: variables in terraform are a great way to define centrally controlled reusable values. The information in Terraform variables is saved independently from the deployment plans, which makes the values easy to read and edit from a single file. This is used mostly when you don’t want to hardcode, so you pass as a variable and make reference (var.)
tfvars: it’s a file used to set the actual values of the variables.
main.tf: This is our main configuration file where resources are defined
variables.tf: is the file where variables are defined.
outputs.tf: This file contains output definitions for our resources.
Type: is the type of resource we want to create, for example, instance
Name: is the name of the identifier we are going to use throughout our terraform code.
Tags: they help in proper identification either for those reading or a system like kubernetes to help it’s cloud control manager
Tfstate: This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures.
Module: module is a container for multiple resources that are used together
From_port: This is the start range for port
To_port: This is the end range of the port
Protocol: The protocol for the port range
Cidr_block: List of CIDR blocks from where you want to allow traffic(CIDR; Classless Inter-Domain Routing)
Provisioners: are used to executing scripts on a local or remote machine as part of resource creation or destruction.
terraform init: initialize a working directory containing terraform configuration files. It should be run after writing a new terraform configuration
terraform plan: This command reviews changes you made on your configuration file, even if there were none.
terraform apply: This command implements the changes you have made, more like an executioner
terraform destroy; this command terminates results managed by your terraform project.
At this point you have understood the difference between Terraform and ansible, also Terraform environment. This would help you when building infrastructures for your project(s).
Kindly check aleqxan.medium.com for our other articles