You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.0 KiB

  1. #!/bin/bash
  2. #
  3. # Generates a inventory file based on the terraform output.
  4. # After provisioning a cluster, simply run this command and supply the terraform state file
  5. # Default state file is terraform.tfstate
  6. #
  7. set -e
  8. usage () {
  9. echo "Usage: $0 <state file>" >&2
  10. exit 1
  11. }
  12. if [[ $# -ne 1 ]]; then
  13. usage
  14. fi
  15. TF_STATE_FILE=${1}
  16. if [[ ! -f "${TF_STATE_FILE}" ]]; then
  17. echo "ERROR: state file ${TF_STATE_FILE} doesn't exist" >&2
  18. usage
  19. fi
  20. TF_OUT=$(terraform output -state "${TF_STATE_FILE}" -json)
  21. MASTERS=$(jq -r '.master_ips.value | to_entries[]' <(echo "${TF_OUT}"))
  22. WORKERS=$(jq -r '.worker_ips.value | to_entries[]' <(echo "${TF_OUT}"))
  23. mapfile -t MASTER_NAMES < <(jq -r '.key' <(echo "${MASTERS}"))
  24. mapfile -t WORKER_NAMES < <(jq -r '.key' <(echo "${WORKERS}"))
  25. API_LB=$(jq -r '.control_plane_lb_ip_address.value' <(echo "${TF_OUT}"))
  26. # Generate master hosts
  27. i=1
  28. for name in "${MASTER_NAMES[@]}"; do
  29. private_ip=$(jq -r '. | select( .key=='"\"${name}\""' ) | .value.private_ip' <(echo "${MASTERS}"))
  30. public_ip=$(jq -r '. | select( .key=='"\"${name}\""' ) | .value.public_ip' <(echo "${MASTERS}"))
  31. echo "${name} ansible_user=ubuntu ansible_host=${public_ip} ip=${private_ip} etcd_member_name=etcd${i}"
  32. i=$(( i + 1 ))
  33. done
  34. # Generate worker hosts
  35. for name in "${WORKER_NAMES[@]}"; do
  36. private_ip=$(jq -r '. | select( .key=='"\"${name}\""' ) | .value.private_ip' <(echo "${WORKERS}"))
  37. public_ip=$(jq -r '. | select( .key=='"\"${name}\""' ) | .value.public_ip' <(echo "${WORKERS}"))
  38. echo "${name} ansible_user=ubuntu ansible_host=${public_ip} ip=${private_ip}"
  39. done
  40. echo ""
  41. echo "[kube_control_plane]"
  42. for name in "${MASTER_NAMES[@]}"; do
  43. echo "${name}"
  44. done
  45. echo ""
  46. echo "[kube_control_plane:vars]"
  47. echo "supplementary_addresses_in_ssl_keys = [ '${API_LB}' ]" # Add LB address to API server certificate
  48. echo ""
  49. echo "[etcd]"
  50. for name in "${MASTER_NAMES[@]}"; do
  51. echo "${name}"
  52. done
  53. echo ""
  54. echo "[kube_node]"
  55. for name in "${WORKER_NAMES[@]}"; do
  56. echo "${name}"
  57. done
  58. echo ""
  59. echo "[k8s_cluster:children]"
  60. echo "kube_control_plane"
  61. echo "kube_node"