Skip to content

Commit

Permalink
Consolidate VM config into list object
Browse files Browse the repository at this point in the history
  • Loading branch information
kencx committed Oct 31, 2023
1 parent 07d58df commit a2af47f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 158 deletions.
32 changes: 26 additions & 6 deletions docs/src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,32 @@ software installed (eg. Docker). For more information, refer to
proxmox_ip = "https://${PVE_IP}:8006/api2/json"
proxmox_api_token = "${API_TOKEN}"
template_id = 5000
server_vmid = [110]
client_vmid = [111]
server_ip_address = ["10.10.10.110/24"]
client_ip_address = ["10.10.10.111/24"]
ip_gateway = "10.10.10.1"
template_id = 5000
ip_gateway = "10.10.10.1"
servers = [
{
name = "server"
id = 110
cores = 2
sockets = 2
memory = 4096
disk_size = 10
ip_address = "10.10.10.110/24"
}
]
clients = [
{
name = "client"
id = 111
cores = 2
sockets = 2
memory = 10240
disk_size = 15
ip_address = "10.10.10.111/24"
}
]
ssh_user = "debian"
ssh_private_key_file = "/path/to/ssh/private/key"
Expand Down
54 changes: 30 additions & 24 deletions docs/src/terraform/proxmox.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,32 @@ client nodes with the given IP addresses. All nodes will be cloned from the
given VM template.

```hcl
template_id = 5000
server_vmid = [110, 111]
client_vmid = [120, 121]
server_ip_address = ["10.10.10.110/24", "10.10.10.111/24"]
client_ip_address = ["10.10.10.120/24", "10.10.10.121/24"]
ip_gateway = "10.10.10.1"
template_id = 5003
ip_gateway = "10.10.10.1"
servers = [
{
name = "server"
id = 110
cores = 2
sockets = 2
memory = 4096
disk_size = 10
ip_address = "10.10.10.110/24"
}
]
clients = [
{
name = "client"
id = 111
cores = 2
sockets = 2
memory = 10240
disk_size = 15
ip_address = "10.10.10.111/24"
}
]
```

On success, the provisioned VMs are accessible via the configured SSH username
Expand Down Expand Up @@ -112,32 +132,18 @@ inventory=../terraform/cluster/tf_ansible_inventory,/path/to/other/inventory/fil
| template_id | Template ID to clone | number | |
| onboot | Start VM on boot | bool | `false` |
| started | Start VM on creation | bool | `true` |
| server_hostname_prefix | Hostname prefix for all server nodes | string | `server` |
| server_vmid | List of server VM IDs | list(number) | |
| server_cores | Number of cores for all server nodes | number | `2` |
| server_sockets | Number of sockets for all server nodes | number | `2` |
| server_memory | Total memory for all server nodes (MB) | number | `2048` |
| server_disk_size | Disk size in all server nodes (GB) | string | `5` |
| client_hostname_prefix | Hostname prefix for all client nodes | string | `client` |
| client_vmid | List of client VM IDs | list(number) | |
| client_cores | Number of cores for all client nodes | number | `2` |
| client_sockets | Number of sockets for all client nodes | number | `2` |
| client_memory | Total memory for all client nodes (MB) | number | `2048` |
| client_disk_size | Disk size in all client nodes (GB) | string | `5` |
| servers | List of server config (see above) | list(object) | `[]` |
| clients | List of client config (see above) | list(object) | `[]` |
| disk_datastore | Datastore on which to store VM disk | string | `volumes` |
| server_ip_address | List of server IPv4 addresses in CIDR notation | list(string) | |
| client_ip_address | List of client IPv4 addresses in CIDR notation | list(string) | |
| control_ip_address | Control IPv4 address in CIDR notation | string | |
| ip_gateway | IPv4 gateway address | string | |
| ssh_username | User to SSH into during provisioning | string | |
| ssh_private_key_file | Filepath of private SSH key | string | |
| ssh_public_key_file | Filepath of public SSH key | string | |

- The VM template corresponding to `template_id` must exist
- The length of `server_vmid` and `server_ip_address` must be equal. Each
element in the latter corresponds to the IP address of the latter. The same
applies for the client arrays
- The lists of IPv4 addresses must be in CIDR notation with subnet masks
- The IPv4 addresses must be in CIDR notation with subnet masks (eg.
`10.0.0.2/24`)

## Notes

Expand Down
67 changes: 28 additions & 39 deletions terraform/cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,26 @@ provider "proxmox" {
}
}

locals {
servers = {
for idx, val in var.server_vmid : val => {
index = idx + 1,
ip_address = var.server_ip_address[idx],
}
}
clients = {
for idx, val in var.client_vmid : val => {
index = idx + 1,
ip_address = var.client_ip_address[idx],
}
}
}

module "server" {
source = "../modules/vm"
for_each = local.servers
source = "../modules/vm"
for_each = {
for idx, vm in var.servers : idx + 1 => vm
}

hostname = "${var.server_hostname_prefix}-${each.value.index}"
vmid = parseint(each.key, 10)
hostname = "server-${each.key}"
vmid = each.value.id
tags = var.tags
target_node = var.target_node

clone_template_id = var.template_id
onboot = var.onboot
started = var.started

cores = var.server_cores
sockets = var.server_sockets
memory = var.server_memory
cores = each.value.cores
sockets = each.value.sockets
memory = each.value.memory

disk_size = var.server_disk_size
disk_size = each.value.disk_size
disk_datastore = var.disk_datastore

ip_address = each.value.ip_address
Expand All @@ -59,23 +46,25 @@ module "server" {
}

module "client" {
source = "../modules/vm"
for_each = local.clients
source = "../modules/vm"
for_each = {
for idx, vm in var.clients : idx + 1 => vm
}

hostname = "${var.client_hostname_prefix}-${each.value.index}"
vmid = parseint(each.key, 10)
hostname = "client-${each.key}"
vmid = each.value.id
tags = var.tags
target_node = var.target_node

clone_template_id = var.template_id
onboot = var.onboot
started = var.started

cores = var.client_cores
sockets = var.client_sockets
memory = var.client_memory
cores = each.value.cores
sockets = each.value.sockets
memory = each.value.memory

disk_size = var.client_disk_size
disk_size = each.value.disk_size
disk_datastore = var.disk_datastore

ip_address = each.value.ip_address
Expand All @@ -88,21 +77,21 @@ module "client" {
resource "local_file" "tf_ansible_inventory_file" {
content = <<-EOF
[server]
%{for ip in var.server_ip_address~}
${split("/", ip)[0]}
%{for vm in var.servers~}
${split("/", vm.ip_address)[0]}
%{endfor~}
[client]
%{for ip in var.client_ip_address~}
${split("/", ip)[0]}
%{for vm in var.clients~}
${split("/", vm.ip_address)[0]}
%{endfor~}
[prod]
%{for ip in var.server_ip_address~}
${split("/", ip)[0]}
%{for vm in var.servers~}
${split("/", vm.ip_address)[0]}
%{endfor~}
%{for ip in var.client_ip_address~}
${split("/", ip)[0]}
%{for vm in var.clients~}
${split("/", vm.ip_address)[0]}
%{endfor~}
EOF
filename = "${path.module}/tf_ansible_inventory"
Expand Down
134 changes: 45 additions & 89 deletions terraform/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,74 +37,30 @@ variable "started" {
default = true
}

variable "server_hostname_prefix" {
type = string
description = "Hostname prefix of server node"
default = "server"
}

variable "client_hostname_prefix" {
type = string
description = "Hostname prefix of client node"
default = "client"
}

variable "server_vmid" {
type = list(number)
description = "List of server VM IDs"
}

variable "client_vmid" {
type = list(number)
description = "List of client VM IDs"
}

variable "server_cores" {
type = number
description = "Number of cores in server nodes"
default = 2
}

variable "server_sockets" {
type = number
description = "Number of sockets in server nodes"
default = 2
}

variable "server_memory" {
type = number
description = "Memory (MB) in server nodes"
default = 2048
}

variable "server_disk_size" {
type = number
description = "Disk size (GB) in server nodes"
default = 5
}

variable "client_cores" {
type = number
description = "Number of cores in client nodes"
default = 2
}

variable "client_sockets" {
type = number
description = "Number of sockets in client nodes"
default = 2
}

variable "client_memory" {
type = number
description = "Memory (MB) in client nodes"
default = 1024
}

variable "client_disk_size" {
type = number
description = "Disk size (GB) in server nodes"
default = 5
variable "servers" {
type = list(object({
name = string
id = number
cores = number
sockets = number
memory = number
disk_size = number
ip_address = string
}))
default = []
}

variable "clients" {
type = list(object({
name = string
id = number
cores = number
sockets = number
memory = number
disk_size = number
ip_address = string
}))
default = []
}

variable "disk_datastore" {
Expand All @@ -113,27 +69,27 @@ variable "disk_datastore" {
default = "volumes"
}

variable "server_ip_address" {
type = list(string)
description = "List of server IPv4 address in CIDR notation (eg. 10.10.10.2/24)"
validation {
condition = alltrue([
for i in var.server_ip_address : can(cidrnetmask(i))
])
error_message = "Must be a valid IPv4 address with subnet mask"
}
}

variable "client_ip_address" {
type = list(string)
description = "List of client IPv4 address in CIDR notation (eg. 10.10.10.2/24)"
validation {
condition = alltrue([
for i in var.client_ip_address : can(cidrnetmask(i))
])
error_message = "Must be a valid IPv4 address with subnet mask"
}
}
# variable "server_ip_address" {
# type = list(string)
# description = "List of server IPv4 address in CIDR notation (eg. 10.10.10.2/24)"
# validation {
# condition = alltrue([
# for i in var.server_ip_address : can(cidrnetmask(i))
# ])
# error_message = "Must be a valid IPv4 address with subnet mask"
# }
# }
#
# variable "client_ip_address" {
# type = list(string)
# description = "List of client IPv4 address in CIDR notation (eg. 10.10.10.2/24)"
# validation {
# condition = alltrue([
# for i in var.client_ip_address : can(cidrnetmask(i))
# ])
# error_message = "Must be a valid IPv4 address with subnet mask"
# }
# }

variable "control_ip_address" {
type = string
Expand Down

0 comments on commit a2af47f

Please sign in to comment.