I’m sure many of you have stumbled across the article Ansible and HashiCorp: Better Together and wondered, how can I do this in my environment? I, like many folks, really wanted to think that Terraform could configure a VM while its deploying the rest of the infrastructure. Y’know, that whole frying of an instance from a baked image. This may work in very simple, caveat free, environments who have little technological baggage. Unfortunately, that’s not the world we all live in. This led me down the path of investigating a potential cooperation between Terraform & Ansible of which there are a few options:
- The Ansible provisioner
- The local-exec method calling ansible from Terraform
- Using a tool to create a dynamic ansible inventory from the
.tfstate
- Create an Ansible inventory from Terraform
I tried several of these methods and the one which worked best for me was to have terraform generate an Ansible inventory. This solution was simple yet functional for our organization. We have no need to maintain the .tfstate
long-term as we’re only needing terraform to stand up several long-running VMs which will be maintained & patched. This may seem like the old-school method of infrastructure as code, because it is, but it works and works well. I’ve created a Github repo as an example.
Terraform templatefile()
The key to getting Terraform to generate a file from the .tfstate
is the templatefile()
function. Several attempts at getting this to work included using a null_resource
with a local-exec but that failed to loop properly when generating the inventory from my template and that’s where templatefile()
came in handy.
HCL Code
1 | ###################################### |
Template hosts.tpl
1 | all: |