provisioning.tf 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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"]
  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" "ansible_tester_inventory" {
  29. count = local.tester_count
  30. template = "${file("${path.module}/inventory.tpl")}"
  31. vars = {
  32. host_ip = element(aws_eip.tester-eip.*.public_ip, count.index)
  33. vpc_name = var.vpc-name
  34. ncount = count.index
  35. }
  36. depends_on = [aws_instance.tester, aws_eip_association.tester-eip-assoc]
  37. }
  38. data "template_file" "ssh_config" {
  39. template = "${file("${path.module}/ssh.tpl")}"
  40. vars = {
  41. vpc_name = var.vpc-name
  42. }
  43. depends_on = [aws_instance.re, aws_eip_association.re-eip-assoc, aws_volume_attachment.re-ephemeral]
  44. }
  45. ###############################################################################
  46. # Template Write
  47. resource "null_resource" "inventory-setup" {
  48. count = var.data-node-count
  49. provisioner "local-exec" {
  50. command = "echo \"${element(data.template_file.ansible_inventory.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.ini"
  51. }
  52. depends_on = [data.template_file.ansible_inventory]
  53. }
  54. resource "null_resource" "tester-inventory-setup" {
  55. count = local.tester_count
  56. provisioner "local-exec" {
  57. command = "echo \"${element(data.template_file.ansible_tester_inventory.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_tester_node_${count.index}.ini"
  58. }
  59. depends_on = [data.template_file.ansible_inventory]
  60. }
  61. resource "null_resource" "ssh-setup" {
  62. provisioner "local-exec" {
  63. command = "echo \"${data.template_file.ssh_config.rendered}\" > /tmp/${var.vpc-name}_node.cfg"
  64. }
  65. depends_on = [data.template_file.ssh_config]
  66. }
  67. ###############################################################################
  68. # Run some ansible
  69. resource "null_resource" "ansible-run" {
  70. count = var.data-node-count
  71. provisioner "local-exec" {
  72. 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 'ENABLE_VOLUMES=${var.enable-volumes}'"
  73. }
  74. depends_on = [null_resource.remote-config]
  75. }
  76. resource "null_resource" "ansible-tester-run" {
  77. count = var.data-node-count
  78. provisioner "local-exec" {
  79. command = "ansible-playbook ${path.module}/ansible/testernode.yml --private-key ${local.ssh_key_path} -i /tmp/${var.vpc-name}_tester_node_${count.index}.ini --become -e 'ENABLE_VOLUMES=${var.enable-volumes}'"
  80. }
  81. depends_on = [null_resource.remote-config]
  82. }