Skip to content

Instantly share code, notes, and snippets.

@charlessolar
Last active January 13, 2020 11:54
Show Gist options
  • Save charlessolar/515016139f0014cdfc029c7dd553d597 to your computer and use it in GitHub Desktop.
Save charlessolar/515016139f0014cdfc029c7dd553d597 to your computer and use it in GitHub Desktop.
ansible inventory files from terraform
### variables
variable "env" {}
variable "riak_count" {}
variable "elastic_count" {}
### hostname.tpl
${name}-${env}-${format("%02s",index)} ${extra}
### ansible.tpl
[riak]
${riak_hosts}
[elastic]
${elastic_hosts}
[${env}:children]
riak
elastic
#### outputs.tf
data "null_data_source" "inventory" {
inputs = {
riak = "${join(",",vsphere_virtual_machine.riak.*.network_interface.0.ipv4_address)}",
elastic = "${join(",", vsphere_virtual_machine.elastic.*.network_interface.0.ipv4_address)}"
}
}
data "template_file" "riak_ansible" {
count = "${var.riak_count}"
template = "${file("${path.module}/hostname.tpl")}"
vars {
index = "${count.index + 1}"
name = "riak"
env = "${var.env}"
extra = " ansible_host=${element(split(",",data.null_data_source.inventory.inputs.riak),count.index)}"
# extra = ""
}
}
data "template_file" "elastic_ansible" {
count = "${var.elastic_count}"
template = "${file("${path.module}/hostname.tpl")}"
vars {
index = "${count.index + 1}"
name = "elastic"
env = "${var.env}"
extra = " ansible_host=${element(split(",",data.null_data_source.inventory.inputs.elastic),count.index)}"
# extra = ""
}
}
data "template_file" "ansible_hosts" {
template = "${file("${path.module}/ansible.tpl")}"
vars {
env = "${var.env}"
riak_hosts = "${join("\n",data.template_file.riak_ansible.*.rendered)}"
elastic_hosts = "${join("\n",data.template_file.elastic_ansible.*.rendered)}"
}
}
output "ansible_hosts" {
value = "${data.template_file.ansible_hosts.rendered}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment