Ansible and Terraform are both amazing tools with different use cases, however, some of their use cases can overlap. For example, both Ansible and Terraform can be used to deploy and configure AWS, Azure, GCP, Cisco ACI or hundreds of other platforms. In most environments, especially mutli-cloud or hybrid environments, you could, and may be should use both tools to get best of both worlds. For example, you’d setup the Infrastructure-as-code (IAC) using Terraform, and then also use a Terraform “provisioner” to invoke an Ansible Playbook to do the required configuration.
Here are some key differences between the two platforms at a high-level.
|Purpose||Primarily designed for configuration management, but can also be utilized as an Infrastructure-as-code (IAC) tool||Entirely designed to be an infrastructure-as-code tool, but, can also handle light configuration activities.|
|State||Ansible does not maintain infrastructure or configuration state||Terraform maintains infrastructure state|
|Idempotency||Is idempotent – but less idempotent than Terraform. For example, if you have a playbook to create an EC2 instance, depending on how it’s written, it may create a new instance.||Is idempotent for the infrastructure that is within it’s state file. Is not idempotent if it’s not in the state file or is not imported into the state file. In the EC2 example, it will NOT create a new instance when a Terraform configuration is applied a second time using the same configuraiton.|
|Complexity||Ansible has a significantly higher learning curve – but is also more configurable.||Much easier to get started with, but, lacks the configurability of Ansible.|
|Platforms Support||Only runs on Linux, Mac and Unix (but it’s easily enough to run a Linux VM if you are on Windows!)||Natively supported on Windows, Linux, Unix, Solaris, Mac, etc.|
|Supported Platforms||Can manage and configure anything that has a CLI or API. |
Functionality, idempotency and features can be enhanced using the Ansible Collections. As of May 4, 2022, there are 31,000 collections
|Terraform natively can only support platforms listed in the Terraform Registry: https://registry.terraform.io/|
As of May 4, 2022, there are 2059 supported platforms
|Troubleshooting||Errors are very cryptic||Errors are very easy to decipher|
|Ease of Configuration||YAML can be a pain to work with – as the spaces, tabs, etc. are significant, and can lead to frustrating errors when off by just a single space character. When everything is correct however, it’s easy to read and decipher.||Hashicorp Configuration Language (HCL) is super easy to read and decipher, and doesn’t suffer from the limitations and frustrations of dealing with YAML. It doesn’t care about spaces or indentation. There is also a nice “terraform fmt” command to keep code clean looking.|