Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way currently to create an ansible inventory from terraform

I have a long list of machines, all of which are a little different in functionality in a system. I'd like to organize these machines and add to a hosts inventory file automatically so I can run ansible and manage inventory. Are there good solutions out there for this?

I think ansible hosts should looks something like...

[webservers] someip someip [integration] someip someip 

etc..

After asking the question, I currently am researching output vars and using those to render a template from a file.

like image 431
DrM Avatar asked Aug 03 '17 16:08

DrM


People also ask

Can terraform and Ansible work together?

Terraform and Ansible Ansible is a configuration-management and application-deployment tool. It means that you'll use Terraform first to create, for example, a virtual machine and then use Ansible to install necessary applications on that machine.

What is terraform inventory?

Terraform Inventory. This is a little Go app which generates a dynamic Ansible inventory from a Terraform state file. It allows one to spawn a bunch of instances with Terraform, then (re-)provision them with Ansible. The following providers are supported: AWS.


1 Answers

I figured it out.

data "template_file" "dev_hosts" {   template = "${file("${path.module}/templates/dev_hosts.cfg")}"   depends_on = [     "aws_instance.dev-api-gateway",     "aws_instance.dev-api-gateway-internal",     ....   ]   vars {     api_public = "${aws_instance.dev-api-gateway.private_ip}"     api_internal = "${aws_instance.dev-api-gateway-internal.private_ip}"   } }  resource "null_resource" "dev-hosts" {   triggers {     template_rendered = "${data.template_file.dev_hosts.rendered}"   }   provisioner "local-exec" {     command = "echo '${data.template_file.dev_hosts.rendered}' > dev_hosts"   } } 

Then create a template in the file referenced earlier

Contents of example dev_hosts.cfg

[public] ${api_public}   [private] ${api_internal} 
like image 166
DrM Avatar answered Sep 23 '22 00:09

DrM