iac: harden node lifecycle — tofu for_each + self-contained ansible rejoin

Implements T-1.5 findings F1/F2 (author-first, no cluster change).

F1 (tofu): refactor libvirt node resources count -> for_each keyed by
node name so single-node -replace/destroy cannot cascade to siblings.
Add ordered-list/primary helper locals; update outputs.tf off the map;
add moved.tf (12 moved blocks). tofu plan proves a pure state-address
migration: 0 to add, 0 to change, 0 to destroy (all 12 'has moved to').
State-persist via a 0-change apply is operator-permission-gated.

F2 (ansible): k3s_server secondary fetches the join token via
delegate_to the primary (published fact as fast path), so a single-node
rejoin with --limit k3s-dev-N is self-contained and no longer depends on
a fact an earlier play had to set. Syntax-check + CI gate green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
jakman85andClaude Opus 4.8 committed 2026-08-16 19:42:12 -05:00
1 parent ef33865a53
commit 95e3eafac6
5 files changed
+150 -38

No files matched your search

+8 -8
View File
@@ -4,27 +4,27 @@
output "node_names" {
description = "Ordered VM hostnames."
value = [for n in local.nodes : n.name]
value = [for n in local.node_list : n.name]
}
output "node_ips" {
description = "Ordered node static IPs (same order as node_names)."
value = [for n in local.nodes : n.ip]
value = [for n in local.node_list : n.ip]
}
output "nodes" {
description = "Per-node map name => ip, for building the Ansible inventory."
value = { for n in local.nodes : n.name => n.ip }
value = { for n in local.node_list : n.name => n.ip }
}
output "primary_node_ip" {
description = "First server IP — k3s --cluster-init host and Cilium k8sServiceHost (parity value)."
value = local.nodes[0].ip
value = local.primary.ip
}
output "k3s_api_endpoint" {
description = "kube-apiserver endpoint used for k8sServiceHost/Port parity (docs/02)."
value = "${local.nodes[0].ip}:6443"
value = "${local.primary.ip}:6443"
}
output "cilium_l2_vip_range" {
@@ -40,10 +40,10 @@ output "ansible_inventory" {
value = <<-EOT
# GENERATED from tofu outputs — do not hand-edit; re-render on IP/name change.
[k3s_primary]
${local.nodes[0].name} ansible_host=${local.nodes[0].ip}
${local.primary.name} ansible_host=${local.primary.ip}
[k3s_secondary]
%{for n in slice(local.nodes, 1, length(local.nodes))~}
%{for n in slice(local.node_list, 1, length(local.node_list))~}
${n.name} ansible_host=${n.ip}
%{endfor~}
@@ -53,6 +53,6 @@ output "ansible_inventory" {
[k3s_servers:vars]
ansible_user=${var.login_user}
k3s_api_host=${local.nodes[0].ip}
k3s_api_host=${local.primary.ip}
EOT
}