init: terraform

This commit is contained in:
ineszang44
2026-09-18 09:48:20 +02:00
parent 667592033b
commit 47c4fd445b
8 changed files with 140 additions and 27 deletions
+20 -13
View File
@@ -7,17 +7,21 @@ locals {
resource "null_resource" "k3s_install" {
triggers = {
ssh_host = var.ssh_host
k3s_version = var.k3s_version
disable_components = join(",", var.k3s_disable_components)
ssh_host = var.ssh_host
ssh_port = tostring(var.ssh_port)
ssh_user = var.ssh_user
ssh_password = var.ssh_password
k3s_version = var.k3s_version
disable_components = join(",", var.k3s_disable_components)
sudo_prefix = local.sudo_prefix
}
connection {
type = "ssh"
host = var.ssh_host
port = var.ssh_port
user = var.ssh_user
private_key = file(var.ssh_private_key_path)
type = "ssh"
host = self.triggers.ssh_host
port = self.triggers.ssh_port
user = self.triggers.ssh_user
password = self.triggers.ssh_password
}
provisioner "remote-exec" {
@@ -33,7 +37,7 @@ resource "null_resource" "k3s_install" {
when = destroy
on_failure = continue
inline = [
"${local.sudo_prefix}sh -c 'test -x /usr/local/bin/k3s-uninstall.sh && /usr/local/bin/k3s-uninstall.sh || true'",
"${self.triggers.sudo_prefix}sh -c 'test -x /usr/local/bin/k3s-uninstall.sh && /usr/local/bin/k3s-uninstall.sh || true'",
]
}
}
@@ -46,10 +50,13 @@ resource "null_resource" "fetch_kubeconfig" {
}
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = <<-EOT
ssh -i "${var.ssh_private_key_path}" -p ${var.ssh_port} -o StrictHostKeyChecking=accept-new ${var.ssh_user}@${var.ssh_host} '${local.kubeconfig_cmd}' \
| sed 's/127.0.0.1/${var.ssh_host}/' > "${var.kubeconfig_output_path}"
interpreter = ["PowerShell", "-NoProfile", "-Command"]
command = <<-EOT
$content = ssh -i "${pathexpand(var.ssh_private_key_path)}" -p ${var.ssh_port} -o StrictHostKeyChecking=accept-new ${var.ssh_user}@${var.ssh_host} "cat /etc/rancher/k3s/k3s.yaml"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$content -replace "127.0.0.1", "${var.ssh_host}" |
Set-Content -Path "${var.kubeconfig_output_path}" -Encoding UTF8
EOT
}
}
+5
View File
@@ -21,6 +21,11 @@ variable "ssh_private_key_path" {
sensitive = true
}
variable "ssh_password" {
type = string
sensitive = true
}
variable "k3s_version" {
type = string
description = "Version k3s a epingler pour un deploiement reproductible (ex: v1.31.5+k3s1). Voir https://github.com/k3s-io/k3s/releases."
+3 -3
View File
@@ -2,9 +2,9 @@ terraform {
required_version = ">= 1.7"
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.2"
provider = {
source = "hashicorp/archive"
version = "~> 2.4"
}
}
}