provisioning.tf 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_instance.re.*.public_ip, count.index)}"
  9. private_key = "${file("~/.ssh/${var.vpc-name}.pem")}"
  10. agent = true
  11. }
  12. inline = ["sudo apt update && sudo apt install -y python python-pip"]
  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_instance.re.*.public_ip, count.index)}"
  23. vpc_name = "${var.vpc-name}"
  24. ncount = "${count.index}"
  25. }
  26. }
  27. data "template_file" "ssh_config" {
  28. count = var.data-node-count
  29. template = "${file("${path.module}/ssh.tpl")}"
  30. vars = {
  31. host_ip = "${element(aws_instance.re.*.public_ip, count.index)}"
  32. vpc_name = "${var.vpc-name}"
  33. ncount = "${count.index}"
  34. }
  35. }
  36. ###############################################################################
  37. # Template Write
  38. resource "null_resource" "inventory-setup" {
  39. count = var.data-node-count
  40. provisioner "local-exec" {
  41. command = "echo \"${element(data.template_file.ansible_inventory.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.ini"
  42. }
  43. depends_on = ["data.template_file.ansible_inventory"]
  44. }
  45. resource "null_resource" "ssh-setup" {
  46. count = var.data-node-count
  47. provisioner "local-exec" {
  48. command = "echo \"${element(data.template_file.ssh_config.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.cfg"
  49. }
  50. depends_on = ["data.template_file.ssh_config"]
  51. }
  52. ###############################################################################
  53. # Run some ansible
  54. resource "null_resource" "ansible-run" {
  55. count = var.data-node-count
  56. provisioner "local-exec" {
  57. command = "ansible-playbook ${path.module}/ansible/playbook.yml --private-key ~/.ssh/${var.vpc-name}.pem -i /tmp/${var.vpc-name}_node_${count.index}.ini --become -e 'MYENV=1'"
  58. }
  59. depends_on = ["null_resource.remote-config"]
  60. }