diff --git a/example.tfvars b/example.tfvars index 4b6100d..9d7f2b9 100644 --- a/example.tfvars +++ b/example.tfvars @@ -1,4 +1,4 @@ -vm-name-prefix = "docker-ansible-test" +vm-name-prefix = "ansible-test" # A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0' # equates to any ip address. @@ -7,12 +7,13 @@ admin-ips = [ "8.8.8.8/32", "0.0.0.0/0" ] disk-image-dir = "/path/to/disk/pool/" libvirt-connection-url = "qemu+ssh://@/system" -master-nodes = 1 -worker-nodes = 2 - node-memory = 2048 node-vcpus = 2 +################################################################################ +# AWS EC2 instance types +################################################################################ + # 1 GiB, 1 vcpu, only one that is free. # This one won't work with k8s because it requires at least 2 vcpus. aws-ec2-instance-type = "t2.micro" @@ -53,6 +54,10 @@ base-image = "ami-0dd0ccab7e2801812" # 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" +################################################################################ +# Keys/Passwords +################################################################################ + # Password hash created with: # python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))' # where "linux" is the password. diff --git a/get-vm-ips.sh b/get-vm-ips.sh index 8eceb68..cbdc820 100755 --- a/get-vm-ips.sh +++ b/get-vm-ips.sh @@ -22,7 +22,7 @@ IPS_JSON="$( # An array of all node "types" NODE_TYPE_ARRAY="$( echo $IPS_JSON | \ - jq '.key' | \ + jq '.value.value | to_entries | .[] | .key' | \ sed 's/"//g' | \ sed -z 's/\n/ /g;s/ $/\n/g')" @@ -32,12 +32,12 @@ VM_IP_EXPORTS="$( # Convert type, converts "master-ips" to "MASTER" TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)" - echo "$OUTPUTS_JSON" | \ - jq '.'"$TYPE"'.value[]' | \ + echo "$IPS_JSON" | \ + jq '.value.value.'"$TYPE"'[]' | \ # Add line numbers starting with 0. nl -v 0 | \ # Print an export string with a type placeholder "__TYPE__". - awk '{print "export __TYPE__" $1 "=" $2}' | \ + awk '{print "export __TYPE___" $1 "=" $2}' | \ sed s/__TYPE__/$TYPE_UPPER/g done)" @@ -48,6 +48,7 @@ ANSIBLE_INV="$( sed -z 's/\n/,/g;s/,$/\n/g')" # Create an inventory file for ansible. +echo "# Wrote an Ansible inventory file at ./inventory" echo "[k8s_nodes]" > inventory echo $VM_IP_EXPORTS | \ sed 's/"//g' | \ diff --git a/main.tf b/main.tf index 49485b4..caa213c 100644 --- a/main.tf +++ b/main.tf @@ -1,85 +1,38 @@ terraform { - required_version = ">= 0.13" + required_version = ">= 1.0.8" required_providers { libvirt = { - source = "dmacvicar/libvirt" + source = "dmacvicar/libvirt" version = "0.6.11" } } } -################################################################################ -# cloud-init -################################################################################ - -# data "template_file" "master-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}-master-${count.index}" -# } -# count = var.master-nodes -# } -# -# data "template_file" "worker-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}-worker-${count.index}" -# } -# count = var.worker-nodes -# } - -data "template_file" "amzn2-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}-amzn2-${count.index}" - } - count = 1 -} - -data "template_file" "ubuntu-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}-ubuntu-${count.index}" - } - 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}" +locals { + nodes-config = { + "master" = { + base-image = var.amzn2-ami + num = 1 + }, + "worker" = { + base-image = var.amzn2-ami + num = 2 + } } - 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 -} +################################################################################ +# cloud-init +################################################################################ -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 +module "cloud-init-config" { + for_each = local.nodes-config + source = "./modules/cloud-init-config" + cloud-init-template = "${path.module}/cloud_init.cfg" + hostname-prefix = "${var.vm-name-prefix}-${each.key}" + num = each.value.num + root-admin-passwd = var.root-admin-passwd + root-admin-pub-key = var.root-admin-pub-key } ################################################################################ @@ -92,16 +45,21 @@ provider "aws" { region = "us-east-2" } -module "aws-amis" { - source = "./modules/aws-amis" -} +# This module will grab the latest ami for a variety of distros. Uncomment to +# get a list of the latest AMIs for our supported distros. +# module "aws-amis" { +# source = "./modules/aws-amis" +# } +# output "amis" { +# value = module.aws-amis.amis +# } module "aws-network" { - source = "./modules/aws-network" - name-prefix = var.vm-name-prefix - vpc-cidr-block = var.aws-vpc-cidr-block + source = "./modules/aws-network" + name-prefix = var.vm-name-prefix + vpc-cidr-block = var.aws-vpc-cidr-block subnet-cidr-block = var.aws-subnet-cidr-block - admin-ips = var.admin-ips + admin-ips = var.admin-ips } # This key pair is not actually used. Keys are added to the nodes via cloud-init @@ -114,87 +72,18 @@ resource "aws_key_pair" "key" { } } -module "amzn2-nodes" { +module "nodes" { + for_each = local.nodes-config source = "./modules/aws-nodes" - ami = var.base-image + ami = each.value.base-image 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.amzn2-node-user-datas - num-nodes = 1 - name-prefix = "${var.vm-name-prefix}-amzn2" + user-datas = lookup(module.cloud-init-config, each.key, null).user-datas + num-nodes = each.value.num + name-prefix = "${var.vm-name-prefix}-${each.key}" } -module "ubuntu-nodes" { - source = "./modules/aws-nodes" - 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] - user-datas = data.template_file.ubuntu-node-user-datas - num-nodes = 1 - 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" -} - -output "amis" { - value = module.aws-amis.amis -} - -# module "master-nodes" { -# source = "./modules/aws-nodes" -# ami = var.base-image -# 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.master-node-user-datas -# num-nodes = var.master-nodes -# name-prefix = "${var.vm-name-prefix}-master" -# } -# -# module "worker-nodes" { -# source = "./modules/aws-nodes" -# ami = var.base-image -# 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.worker-node-user-datas -# num-nodes = var.worker-nodes -# name-prefix = "${var.vm-name-prefix}-worker" -# } - ################################################################################ # end aws ################################################################################ @@ -209,32 +98,19 @@ output "amis" { # uri = var.libvirt-connection-url # } # -# module "master-nodes" { +# module "nodes" { +# for_each = local.nodes-config # source = "./modules/libvirt-nodes" # pool-name = libvirt_pool.images.name -# name-prefix = "${var.vm-name-prefix}-master" -# num-nodes = var.master-nodes +# name-prefix = "${var.vm-name-prefix}-${each.key}" +# num-nodes = each.value.num # node-memory = var.node-memory # node-vcpus = var.node-vcpus -# base-image = var.base-image +# base-image = each.value.base-image # root-admin-passwd = var.root-admin-passwd # root-admin-pub-key = var.root-admin-pub-key # libvirt-connection-url = var.libvirt-connection-url -# user-datas = data.template_file.master-node-user-datas -# } -# -# module "worker-nodes" { -# source = "./modules/libvirt-nodes" -# pool-name = libvirt_pool.images.name -# name-prefix = "${var.vm-name-prefix}-worker" -# num-nodes = var.worker-nodes -# node-memory = var.node-memory -# node-vcpus = var.node-vcpus -# base-image = var.base-image -# root-admin-passwd = var.root-admin-passwd -# root-admin-pub-key = var.root-admin-pub-key -# libvirt-connection-url = var.libvirt-connection-url -# user-datas = data.template_file.worker-node-user-datas +# user-datas = lookup(module.cloud-init-config, each.key, null).user-datas # } # # resource "libvirt_pool" "images" { @@ -247,31 +123,6 @@ output "amis" { # end libvirt ################################################################################ -output "amzn2-ips" { - value = module.amzn2-nodes.ips -} - -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 +output "ips" { + value = { for type, node in module.nodes : type => node.ips } } - -# TODO REM move to other file? -# output "master-ips" { -# value = module.master-nodes.ips -# } -# -# output "worker-ips" { -# value = module.worker-nodes.ips -# } diff --git a/modules/cloud-init-config/main.tf b/modules/cloud-init-config/main.tf new file mode 100644 index 0000000..e1c75c4 --- /dev/null +++ b/modules/cloud-init-config/main.tf @@ -0,0 +1,9 @@ +data "template_file" "user-datas" { + template = file("${var.cloud-init-template}") + vars = { + admin-passwd = "${var.root-admin-passwd}" + admin-pub-key = "${var.root-admin-pub-key}" + hostname = "${var.hostname-prefix}-${count.index}" + } + count = var.num +} diff --git a/modules/cloud-init-config/outputs.tf b/modules/cloud-init-config/outputs.tf new file mode 100644 index 0000000..15415d4 --- /dev/null +++ b/modules/cloud-init-config/outputs.tf @@ -0,0 +1,3 @@ +output "user-datas" { + value = data.template_file.user-datas +} diff --git a/modules/cloud-init-config/variables.tf b/modules/cloud-init-config/variables.tf new file mode 100644 index 0000000..fc4f437 --- /dev/null +++ b/modules/cloud-init-config/variables.tf @@ -0,0 +1,22 @@ +variable "cloud-init-template" { + default = "../../cloud_init.cfg" + description = "The path to the cloud-init config template." + type = string +} + +variable "hostname-prefix" { + description = "This prefix wil be applied as a prefix for the hostnames." +} + +variable "num" { + description = "The number of user-datas to create with these parameters." +} + +variable "root-admin-passwd" { + description = "This value will be substituted for any occurence of 'admin-password' in the cloud-init config template." +} + +variable "root-admin-pub-key" { + description = "This value will be substituted for any occurence of 'admin-pub-key' in the cloud-init config template." +} + diff --git a/modules/libvirt-nodes/main.tf b/modules/libvirt-nodes/main.tf index 9b792d0..cbd0dac 100644 --- a/modules/libvirt-nodes/main.tf +++ b/modules/libvirt-nodes/main.tf @@ -8,10 +8,6 @@ terraform { } } -provider "libvirt" { - uri = var.libvirt-connection-url -} - resource "libvirt_volume" "node-images" { name = "${var.name-prefix}-${count.index}" pool = var.pool-name diff --git a/variables.tf b/variables.tf index 52cf7a2..3719268 100644 --- a/variables.tf +++ b/variables.tf @@ -71,3 +71,39 @@ variable "vm-name-prefix" { default = "k8s-tf" description = "This prefix will appear before all VM names and hostnames, ie. k8s-tf-master-0." } + +################################################################################ +# AWS AMI vars +# These variables are really mor like constants. Using variables improves +# readability. The defaults are manually updated. Use the aws-amis module to get +# the latest for each distro. +################################################################################ + +variable "amzn2-ami" { + default = "ami-0dd0ccab7e2801812" + description = "The AMI to use for Amazon Linux 2." +} +variable "ubuntu-ami" { + default = "ami-06c7d6c0987eaa46c" + description = "The AMI to use for Ubuntu." +} +variable "centos7-ami" { + default = "ami-00f8e2c955f7ffa9b" + description = "The AMI to use for CentOS 7." +} +variable "centos8-ami" { + default = "ami-057cacbfbbb471bb3" + description = "The AMI to use for CentOS 8." +} +variable "arch-ami" { + default = "ami-02653f06de985e3ba" + description = "The AMI to use for Arch Linux." +} +variable "rhel7-ami" { + default = "ami-0a509b3c2a4d05b3f" + description = "The AMI to use for RHEL 7." +} +variable "rhel8-ami" { + default = "ami-0d871ca8a77af2948" + description = "The AMI to use for RHEL 8." +}