Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created March 29, 2022 16:27
Show Gist options
  • Save mttjohnson/5956d56db120b136f660748774173b0f to your computer and use it in GitHub Desktop.
Save mttjohnson/5956d56db120b136f660748774173b0f to your computer and use it in GitHub Desktop.
Generating Ansible Inventory YAML file from Terraform
resource "local_file" "ansible_inventory" {
content = yamlencode(
{
"all": {
"vars": {
"infrastructure": "${local.x_inf_tag}"
"implementation": "${var.implementation_tag}"
"implementation_id": "${random_id.implementation.hex}"
}
"children": {
"xxxxxx_group_xxxxxx": {
"children": {
"xxxxxx_group_xxxxxx_clones": {
"hosts": zipmap(
slice(
aws_instance.server.*.tags[*].AnsibleInventoryName,
1,
data.terraform_remote_state.grid.outputs.az_count
),
slice(
[
for each_ip in aws_eip.server.*.public_ip:
{ "ansible_host": each_ip }
],
# formatlist("{ansible_host: %s}", aws_eip.server.*.public_ip),
1,
data.terraform_remote_state.grid.outputs.az_count
),
)
},
"xxxxxx_group_xxxxxx_reference": {
"hosts": {
"${element(aws_instance.server.*.tags[*].AnsibleInventoryName, 0)}": {
"ansible_host" = "${element(aws_eip.server.*.public_ip, 0)}"
}
},
}
}
}
}
}
}
)
filename = "${var.ansible_inventory_path}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment