From 17d70ea35181f44a4070d62fcf344d2a044dc62c Mon Sep 17 00:00:00 2001 From: Curtis Wilson Date: Thu, 18 Nov 2021 11:19:59 -0500 Subject: [PATCH] Moved node-config to be local, added AMI vars. --- example.tfvars | 13 ------------- get-vm-ips.sh | 1 + main.tf | 22 ++++++++++++++++++---- variables.tf | 41 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/example.tfvars b/example.tfvars index df3f604..41ca648 100644 --- a/example.tfvars +++ b/example.tfvars @@ -21,19 +21,6 @@ aws-ec2-instance-type = "t2.micro" # 4 GiB, 2 vcpus # aws-ec2-instnce-type = "t2.medium" -################################################################################ - -nodes-config = { - "master" = { - base-image = "ami-0dd0ccab7e2801812" - num = 1 - }, - "worker" = { - base-image = "ami-0dd0ccab7e2801812" - num = 2 - } -} - ################################################################################ # AWS images (AMIs) ################################################################################ diff --git a/get-vm-ips.sh b/get-vm-ips.sh index 230b5cf..cbdc820 100755 --- a/get-vm-ips.sh +++ b/get-vm-ips.sh @@ -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 7510f3e..caa213c 100644 --- a/main.tf +++ b/main.tf @@ -8,12 +8,25 @@ terraform { } } +locals { + nodes-config = { + "master" = { + base-image = var.amzn2-ami + num = 1 + }, + "worker" = { + base-image = var.amzn2-ami + num = 2 + } + } +} + ################################################################################ # cloud-init ################################################################################ module "cloud-init-config" { - for_each = var.nodes-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}" @@ -32,7 +45,8 @@ provider "aws" { region = "us-east-2" } -# This module will grab the latest ami for a variety of distros. +# 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" # } @@ -59,7 +73,7 @@ resource "aws_key_pair" "key" { } module "nodes" { - for_each = var.nodes-config + for_each = local.nodes-config source = "./modules/aws-nodes" ami = each.value.base-image ec2-instance-type = var.aws-ec2-instance-type @@ -85,7 +99,7 @@ module "nodes" { # } # # module "nodes" { -# for_each = var.nodes-config +# for_each = local.nodes-config # source = "./modules/libvirt-nodes" # pool-name = libvirt_pool.images.name # name-prefix = "${var.vm-name-prefix}-${each.key}" diff --git a/variables.tf b/variables.tf index e8f3211..3719268 100644 --- a/variables.tf +++ b/variables.tf @@ -43,11 +43,6 @@ variable "node-vcpus" { type = number } -variable "nodes-config" { - description = "A config that declares how many nodes of each type you want created." - type = map(object({base-image=string,num=number})) -} - variable "root-admin-passwd" { description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module." } @@ -76,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." +}