Ansible & Hashicorp Terraform

Automatic Creation of Inventory from Terraformer

To use ansible on the resources provisioned by Terraformer, ansible needs to know the ips of the provisioned resources.

We will be leveraging the local provider. This will allow us to create the inventory file using the output of terraformer.

Add the following block to end of outputs.tf

...

resource "local_file" "AnsibleInventory" {
 content = templatefile("inventory.tmpl",
 {
  droplet-ip = digitalocean_droplet.personal-automated.ipv4_address
 }
 )
 filename = "../inventory"
} 

This requires the template file inventory.tmpl which should be created with the below contents

[personal-automated]
${droplet-ip} 

Here, I am provisioning a digitalocean_droplet with the resource name personal-automated, which is why you see those referenced above.

After adding the local_file provider, you will need to run terraformer init to install it.

terraformer init

As usual, you can apply the changes using

terraformer apply

The changes for this post can be referred to in this pull request.

The Repository at this point of time will look something like this.

Next Steps

Now, we need to setup ansible and run some playbooks on the newly minted server.

Project Page

References

https://www.linkbynet.com/produce-an-ansible-inventory-with-terraform

https://www.opensourcerers.org/2020/10/12/ansible-and-terraform-integration/

Related Posts

2 thoughts on “Automatic Creation of Inventory from Terraformer

Leave a Reply

%d bloggers like this: