From 6aed115478a31990766126d5405a40f9601e5401 Mon Sep 17 00:00:00 2001 From: Curtis Wilson Date: Tue, 16 Nov 2021 14:36:57 -0500 Subject: [PATCH] Added CentOS 7 & 8 and Arch --- .gitignore | 1 + example.tfvars | 6 ++++ get-vm-ips.sh | 63 +++++++++++++++++++++++++++++++++-------- main.tf | 77 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 135 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index b47f542..7103111 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,5 @@ terraform.rc k8s-key* STARTHERE +inventory diff --git a/example.tfvars b/example.tfvars index 37c353f..4b6100d 100644 --- a/example.tfvars +++ b/example.tfvars @@ -30,6 +30,12 @@ aws-ec2-instance-type = "t2.micro" # AWS Amazon Linux 2 AMI (HVM), SSD Volume Type - us-east-2 - 2021.11.12 - free base-image = "ami-0dd0ccab7e2801812" +## CentOS +# CentOS 7.9.2009 x86_64 - us-east-2 - 2021-11-15 +# base-image = "ami-00f8e2c955f7ffa9b" +# CentOS 8.4.2105 x86_64 - us-east-2 - 2021-11015 +# base-image = "ami-057cacbfbbb471bb3" + ## Ubuntu # Ubuntu Server 20.04 LTS (HVM), SSD Volume Type # us-east-2 - (64-bit x86) - 2021.11.12 - free diff --git a/get-vm-ips.sh b/get-vm-ips.sh index 25e36fe..8eceb68 100755 --- a/get-vm-ips.sh +++ b/get-vm-ips.sh @@ -1,19 +1,60 @@ #!/bin/sh +# This script will create environment variables for all of the output IPs. It +# will also create a `ANSIBLE_INV` variable that will be a comma separated +# string of all the IPs. A anisble inventory file called "inventory is created +# as well. +# # Use eval $(./get-vm-ips.sh) to set env vars for ips. terraform refresh > /dev/null -IPS_JSON="$(terraform show -json | jq '.values.outputs')" +# All terraform outputs in json format. +OUTPUTS_JSON="$( + terraform show -json | \ + jq '.values.outputs' | \ + sed 's/-/_/g')" +# Just the IP address outputs in json format. Also all '-' characters are +# replaced by '_' becuase '-' causes jq some problems. +IPS_JSON="$( + echo $OUTPUTS_JSON | \ + jq 'to_entries | .[] | select(.key | contains("ips"))')" +# An array of all node "types" +NODE_TYPE_ARRAY="$( + echo $IPS_JSON | \ + jq '.key' | \ + sed 's/"//g' | \ + sed -z 's/\n/ /g;s/ $/\n/g')" -echo $IPS_JSON | \ - jq '."master-ips".value[]' | \ - nl -v 0 | \ - awk '{print "export MASTER" $1 "=" $2}' | \ - sed 's/"//g' +# Loop over all the node types and create an export line for each IP. +VM_IP_EXPORTS="$( + for TYPE in $NODE_TYPE_ARRAY; do -echo $IPS_JSON | \ - jq '."worker-ips".value[]' | \ - nl -v 0 | \ - awk '{print "export WORKER" $1 "=" $2}' | \ - sed 's/"//g' + # Convert type, converts "master-ips" to "MASTER" + TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)" + echo "$OUTPUTS_JSON" | \ + jq '.'"$TYPE"'.value[]' | \ + # Add line numbers starting with 0. + nl -v 0 | \ + # Print an export string with a type placeholder "__TYPE__". + awk '{print "export __TYPE__" $1 "=" $2}' | \ + sed s/__TYPE__/$TYPE_UPPER/g + done)" + +ANSIBLE_INV="$( + echo "$VM_IP_EXPORTS" | \ + sed 's/"//g' | \ + sed 's/^.*=//g' | \ + sed -z 's/\n/,/g;s/,$/\n/g')" + +# Create an inventory file for ansible. +echo "[k8s_nodes]" > inventory +echo $VM_IP_EXPORTS | \ + sed 's/"//g' | \ + sed 's/export //g' | \ + sed 's/ /\n/g' | \ + sed 's/^\(.*\)\(=.*\)$/\1 ansible_host\2/g' \ + >> inventory + +echo $VM_IP_EXPORTS | sed 's/" /"\n/g' +echo export ANSIBLE_INV=$ANSIBLE_INV diff --git a/main.tf b/main.tf index f8e8418..50982d6 100644 --- a/main.tf +++ b/main.tf @@ -52,6 +52,36 @@ data "template_file" "ubuntu-node-user-datas" { count = 1 } +data "template_file" "arch-node-user-datas" { + template = file("${path.module}/cloud_init.cfg") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "${var.vm-name-prefix}-arch-${count.index}" + } + count = 1 +} + +data "template_file" "centos7-node-user-datas" { + template = file("${path.module}/cloud_init.cfg") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "${var.vm-name-prefix}-centos7-${count.index}" + } + count = 1 +} + +data "template_file" "centos8-node-user-datas" { + template = file("${path.module}/cloud_init.cfg") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "${var.vm-name-prefix}-centos8-${count.index}" + } + count = 1 +} + ################################################################################ # aws # To use the aws module, uncomment the aws modules/resources and comment out the @@ -93,7 +123,7 @@ module "amzn2-nodes" { module "ubuntu-nodes" { source = "./modules/aws-nodes" - ami = var.base-image + ami = "ami-0629230e074c580f2" ec2-instance-type = var.aws-ec2-instance-type subnet-id = module.aws-network.subnet.id security-group-ids = [module.aws-network.default-security-group.id] @@ -102,6 +132,39 @@ module "ubuntu-nodes" { name-prefix = "${var.vm-name-prefix}-ubuntu" } +module "arch-nodes" { + source = "./modules/aws-nodes" + ami = "ami-02653f06de985e3ba" + ec2-instance-type = var.aws-ec2-instance-type + subnet-id = module.aws-network.subnet.id + security-group-ids = [module.aws-network.default-security-group.id] + user-datas = data.template_file.ubuntu-node-user-datas + num-nodes = 1 + name-prefix = "${var.vm-name-prefix}-arch" +} + +module "centos7-nodes" { + source = "./modules/aws-nodes" + ami = "ami-00f8e2c955f7ffa9b" + ec2-instance-type = var.aws-ec2-instance-type + subnet-id = module.aws-network.subnet.id + security-group-ids = [module.aws-network.default-security-group.id] + user-datas = data.template_file.ubuntu-node-user-datas + num-nodes = 1 + name-prefix = "${var.vm-name-prefix}-centos7" +} + +module "centos8-nodes" { + source = "./modules/aws-nodes" + ami = "ami-057cacbfbbb471bb3" + ec2-instance-type = var.aws-ec2-instance-type + subnet-id = module.aws-network.subnet.id + security-group-ids = [module.aws-network.default-security-group.id] + user-datas = data.template_file.ubuntu-node-user-datas + num-nodes = 1 + name-prefix = "${var.vm-name-prefix}-centos8" +} + # module "master-nodes" { # source = "./modules/aws-nodes" # ami = var.base-image @@ -184,6 +247,18 @@ output "ubuntu-ips" { value = module.ubuntu-nodes.ips } +output "arch-ips" { + value = module.arch-nodes.ips +} + +output "centos7-ips" { + value = module.centos7-nodes.ips +} + +output "centos8-ips" { + value = module.centos8-nodes.ips +} + # TODO REM move to other file? # output "master-ips" { # value = module.master-nodes.ips