provisioning.tf 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ##########################################################################################
  2. # ensure we can get to the node first
  3. resource "null_resource" "remote-config" {
  4. count = var.data-node-count
  5. provisioner "remote-exec" {
  6. connection {
  7. user = "ubuntu"
  8. host = "${element(aws_eip.re-eip.*.public_ip, count.index)}"
  9. private_key = "${file(local.ssh_key_path)}"
  10. agent = true
  11. }
  12. inline = ["sudo apt update > /dev/null && sudo apt install -y python python-pip > /dev/null"]
  13. }
  14. depends_on = ["aws_instance.re", "aws_eip_association.re-eip-assoc", "null_resource.inventory-setup", "null_resource.ssh-setup"]
  15. }
  16. ###############################################################################
  17. # Template Data
  18. data "template_file" "ansible_inventory" {
  19. count = var.data-node-count
  20. template = "${file("${path.module}/inventory.tpl")}"
  21. vars = {
  22. host_ip = "${element(aws_eip.re-eip.*.public_ip, count.index)}"
  23. vpc_name = "${var.vpc-name}"
  24. ncount = "${count.index}"
  25. }
  26. depends_on = ["aws_instance.re", "aws_eip_association.re-eip-assoc", "aws_volume_attachment.re-ephemeral"]
  27. }
  28. data "template_file" "ssh_config" {
  29. template = "${file("${path.module}/ssh.tpl")}"
  30. vars = {
  31. vpc_name = "${var.vpc-name}"
  32. }
  33. depends_on = ["aws_instance.re", "aws_eip_association.re-eip-assoc", "aws_volume_attachment.re-ephemeral"]
  34. }
  35. ###############################################################################
  36. # Template Write
  37. resource "null_resource" "inventory-setup" {
  38. count = var.data-node-count
  39. provisioner "local-exec" {
  40. command = "echo \"${element(data.template_file.ansible_inventory.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.ini"
  41. }
  42. depends_on = ["data.template_file.ansible_inventory"]
  43. }
  44. resource "null_resource" "ssh-setup" {
  45. provisioner "local-exec" {
  46. command = "echo \"${data.template_file.ssh_config.rendered}\" > /tmp/${var.vpc-name}_node.cfg"
  47. }
  48. depends_on = ["data.template_file.ssh_config"]
  49. }
  50. ###############################################################################
  51. # Run some ansible
  52. resource "null_resource" "ansible-run" {
  53. count = var.data-node-count
  54. provisioner "local-exec" {
  55. command = "ansible-playbook ${path.module}/ansible/playbook.yml --private-key ${local.ssh_key_path} -i /tmp/${var.vpc-name}_node_${count.index}.ini --become -e 'MYENV=1'"
  56. }
  57. depends_on = ["null_resource.remote-config"]
  58. }