From 8724cede199140571a74cf9c8ea6aa8c98ed480a Mon Sep 17 00:00:00 2001 From: shnee Date: Tue, 16 Nov 2021 14:38:06 -0500 Subject: [PATCH] Create ansible invetory with get-vm-ips.sh --- .gitignore | 1 + example.tfvars | 27 ++++++++++++++++++++++ get-vm-ips.sh | 63 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 80 insertions(+), 11 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 33a78d3..4b6100d 100644 --- a/example.tfvars +++ b/example.tfvars @@ -1,3 +1,5 @@ +vm-name-prefix = "docker-ansible-test" + # A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0' # equates to any ip address. admin-ips = [ "8.8.8.8/32", "0.0.0.0/0" ] @@ -18,10 +20,35 @@ aws-ec2-instance-type = "t2.micro" # 4 GiB, 2 vcpus # aws-ec2-instnce-type = "t2.medium" +################################################################################ +# AWS images (AMIs) +################################################################################ + +## Amazon Linux 2 # AWS Amazon Linux 2 AMI (HVM), SSD Volume Type - Oregon - 2021.11.11 - free # base-image = "ami-00be885d550dcee43" # 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 +# base-image = "ami-0629230e074c580f2" + +## Arch linux +# arch-linux-lts-hvm-2021.06.02.x86_64-ebs - us-east-2 +# base-image = "ami-02653f06de985e3ba" + +################################################################################ +# libvirt images +################################################################################ + # base-image = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img" # From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52 # base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2" 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