Created
March 29, 2022 16:27
-
-
Save mttjohnson/5956d56db120b136f660748774173b0f to your computer and use it in GitHub Desktop.
Generating Ansible Inventory YAML file from Terraform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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