Providing Terraform with that Ansible Magic

March 20, 2023 by Nuno Martins

ansible terraform magic blog

Late last year, we introduced a Red Hat Ansible Certified Collection Collection for Terraform. This was an important step in automation, as these two tools really are great together and leveraging Ansible's ability to orchestrate other tools in the enterprise made this a no-brainer. Terraform with its infrastructure as code (IaC) provisioning and Ansible’s strength in configuration as code are a synergy that cannot be ignored - we are better together! Organizations are now in the position to utilize their existing infrastructure as code manifests and extend their automation with Terraform and Ansible together.  

Now, we are back  with help from our partners at Kyndryl and XLAB and adding more value and magic to infrastructure as code - This time we have some extra muscle with an addition to the Red Hat Ansible Certified Content Collection: The Ansible provider for Terraform.

So what does the provider help us with?

Without a provider, we would need to rely on inventory plugins for the different cloud platforms and use filters to grab instance information from our freshly "Terraformed" infrastructure. This allows us to update our inventory so we can run automated tasks against these hosts. This is pretty smooth in a workflow especially if you are using the automation controller with a workflow. However, this scenario is not without complexity, and what about the Terraform users who are not working with automation controller? How can we leverage Ansible and bring these two tools together? The Ansible provider for Terraform is here to help us!

With the Ansible provider in the Collection, we are able to define the use of an Ansible inventory in the file and once the project is initialized and built by Terraform, we can gather Terraform resource information from the state file and push it into an inventory.

Let’s look a bit closer:


terraform {
  required_providers {                     #### ansible provider
    ansible = {
      version = "~> 0.0.1"
      source  = ""
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"

resource "ansible_host" "my_ec2" {          #### ansible host details
  name   = aws_instance.my_ec2.public_dns
  groups = ["nginx"]
  variables = {
    ansible_user                 = "ansible",
    ansible_ssh_private_key_file = "~/.ssh/id_rsa",
    ansible_python_interpreter   = "/usr/bin/python3"

Using the provider in the allows us to indicate that we want to use an Ansible inventory and allows us to specify Ansible host details for the inventory. Terraform can then initialize and plan the project and embed the details. If we look at the resulting Terraform state file we can see host details defined:

…terraform.tfstate                      #### Inside

"mode": "managed",
      "type": "ansible_host",
      "name": "my_ec2",
      "provider": "provider[\"\"]",
      "instances": [
          "schema_version": 0,
          "attributes": {
            "groups": [
            "id": "",
            "name": "",
            "variables": {
              "ansible_python_interpreter": "/usr/bin/python3",
              "ansible_ssh_private_key_file": "~/.ssh/id_rsa",
              "ansible_user": "ansible"

Taking a deeper look at the inventory, we can see that the plugin has populated instance data from the defined resource in the Terraform state file.

plugin: cloud.terraform.terraform_provider
ansible-inventory -i inventory.yml --graph --vars

  |  |
  |  |  |--{ansible_python_interpreter = /usr/bin/python3}
  |  |  |--{ansible_ssh_private_key_file = ~/.ssh/id_rsa}
  |  |  |--{ansible_user = ubuntu}

We are now able to run playbooks against this inventory and automate the configuration or additional post-provisioning tasks on our hosts without any hassle.

Step 1: …terraform plan
Step 2: …terraform apply 

…Deploying with Terraform…

::::Magic Time::::

Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
++ ansible-playbook -i inventory.yml playbook.yml

PLAY [Install nginx on remote host] *****************************************************************************************

TASK [wait_for_connection] **************************************************************************************************
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:jRqiAGPDzuYGe+l7jNsmQays2qb/C/SJqtnH6pc42ns.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
ok: []

TASK [setup] ****************************************************************************************************************
ok: []

TASK [Install nginx] ********************************************************************************************************
changed: []

TASK [Start nginx] **********************************************************************************************************
ok: []

PLAY RECAP ****************************************************************************************************************** : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

This new provider is extremely useful when you are using Terraform for deployments while leveraging Ansible for cloud operations like application deployments and CI/CD pipelines, Lifecycle management and enforcement, OS patching and maintenance. With this provider being part of the Red Hat Ansible Certified Content Collection, we also have ongoing maintenance and support available! 


What can I do next?

Whether you are beginning your automation journey or are a seasoned veteran, there are a variety of resources to enhance your automation knowledge:


Ansible, Ansible content collections


Nuno Martins

Nuno is a Technical Marketing Manager for the Ansible Automation Platform. He is a Red Hat Certified Architect and a Certified Instructor with over 15 years of experience in multiple technologies. Currently based in South Africa, he has international experience with having worked all over Europe and Africa.


See All

rss-icon  RSS Feed