Skip to content

Instantly share code, notes, and snippets.

@waxb
Created March 30, 2021 10:55
Show Gist options
  • Save waxb/70d61a942b152c00564d31d8e420b11b to your computer and use it in GitHub Desktop.
Save waxb/70d61a942b152c00564d31d8e420b11b to your computer and use it in GitHub Desktop.
Render ansible inventory from terraform
#To templates dir to render from
${ansvars}
${group}
${servers}
#To VM module main.tf
data "template_file" "inventory" {
template = file("${path.module}/templates/ansible_inv.tpl")
depends_on = [
azurerm_virtual_machine.vm,
azurerm_network_interface.vm_nix,
]
vars = {
ansvars = "[${var.group_name}:vars]\nansible_user = ${var.admin_username}"
group = "[${var.group_name}]"
servers = join("\n", azurerm_public_ip.vm-pip.*.ip_address) #azurerm_network_interface.vm-nix.*.private_ip_address
}
}
resource "null_resource" "ansible" {
triggers = {
template_rendered = data.template_file.inventory.rendered
}
provisioner "local-exec" {
command = "echo '${data.template_file.inventory.rendered}' >> inventory"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment