Terraform to create Ansible-Inverntory

When working with a combination of Terraform and Ansible I came across terraform-inventory which does not fit for my purposes to have a ansible-hostgroup per module call with X=3 EC2 instances.

So I came up with my own solution.
The following will create a local_file on the machine which runs terraform apply . In this case under ansible/ansible_inventory .

You can than use it to run your playbooks agains those ansible-hostgroups .

Terraform needs to output (in my case) the ec2-names ec2_name_output which comes from my Terraform-module and the ec2-eip or ec2-pip (private-IP) when you dont have DNS pointing to those instances in place.

When you have those outputs simply do a content with a formated list:

resource "local_file" "ansible_inventory" {
count = "${length(module.elasticsearch.ec2_name_output)}"
content = "[${module.elasticsearch.ec2_purpose}]\n${join("\n",
"%s ansible_host=%s",
"%s ansible_host=%s",
"%s ansible_host=%s",
filename = "ansible/ansible_inventory"

We are using the terraform provider local_file which will create a file with the content we specifiy below

resource "local_file" "ansible_inventory"
count = "${length(module.elasticsearch.ec2_name_output)}"

This will give you the length of all ec2-names so we can than iterate through every entry in our variable list(s).

%s is the placeholder for the following variables and can be used for as many variables you want to have.

%s ansible_host=%s will be followed by the variables which should replace the placeholders.

This results in:

elasticsearch-01 ansible_host=
elasticsearch-02 ansible_host=
elasticsearch-03 ansible_host=
logstash-01 ansible_host=
logstash-02 ansible_host=
logstash-03 ansible_host=
kibana-01 ansible_host=
kibana-02 ansible_host=
kibana-03 ansible_host=

Now you can run your playbook against your instances.

ansible-playbook -i ansible/ansible_inventory playbook.yml

  • In the shown example you have to make sure that all your modules are creating the same amount of instances…
    Any suggestions how to get rid of this are very welcome.


Working as a IT-Operations engineer at NeXenio, a spin-off by Hasso-Plattner-Institute for products around a digitial workspace.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store