From 302b227e045e0898c34657476fe52f2d4cd765c9 Mon Sep 17 00:00:00 2001 From: jerome Date: Mon, 27 Jan 2025 11:30:15 +0100 Subject: local_script --- local_script | 3 +++ main.tf | 61 +++++++++++++++++++++++++++++++++++++++++++----------------- variables.tf | 27 +++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 17 deletions(-) create mode 100755 local_script create mode 100644 variables.tf diff --git a/local_script b/local_script new file mode 100755 index 0000000..3994e4f --- /dev/null +++ b/local_script @@ -0,0 +1,3 @@ +#!/bin/bash +ssh-keygen -f ~/.ssh/known_hosts -R $1 +ssh-keyscan -H $1 >> ~/.ssh/known_hosts diff --git a/main.tf b/main.tf index b131d51..dcad586 100644 --- a/main.tf +++ b/main.tf @@ -1,8 +1,9 @@ terraform { required_providers { proxmox = { - source = "telmate/proxmox" - version = ">=2.0.0" + source = "telmate/proxmox" + #version = "3.0.1-rc6" + version = "2.9.14" } } } @@ -10,30 +11,56 @@ terraform { provider "proxmox" { # Configuration options #pm_tls_insecure = true - pm_api_url = "https://pve.example.com:8006/api2/json" - #pm_user = "root@pam" - #pm_password = "secret" - pm_api_token_id = "root@pam!terraform" - pm_api_token_secret = "350039e6-3f20-4fc2-856d-xxxxd7d" + pm_api_url = "https://pve.xlinfo.fr:8006/api2/json" + pm_user = "stagiaire@pve" + pm_password = "terraform" + #pm_api_token_id = "root@pam!terraform" + #pm_api_token_secret = "fd186c1f-ede9-4ff9-97bb-012410cb15f5" } -resource "proxmox_lxc" "basic" { - target_node = "pve" - hostname = "alpine" - ostemplate = "local:vztmpl/alpine-3.17-default_20221129_amd64.tar.xz" - password = "secret" - unprivileged = true - start = true +resource "proxmox_lxc" "stagiaire" { + target_node = "pve" + count = var.nb + vmid = var.prem_id + count.index + hostname = element(var.prenom, count.index) + ostemplate = var.template + password = "secret" + unprivileged = true + start = true + ssh_public_keys = <<-EOT + ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCZJG/XcIIvW0JOJb6ftEpopS4szOo8dRehASGIOAswPRko6JFMT9QuAvl9YkmzgSorn0KpyQrqAxNTklADocMGPW2BJzKb/1fQyZYKY9bLXxyKaYZKbDZNaUJmW92ThDmUrIWPgjh5BhUxOTwRbDhTsRu/pvulnGw+8yOp7Tz8nUgAoJEZ/7fGkm7AaJPLmF/szQEhL/WSNqTtNdCHNYpQqgRIUZh5zqcb2jXa0pZ7GMnPmoSUMlz1OfAxMOIuziaP3i1J/KHVhXdxj4nrOtUjrUULfqk9vyfKkf7BLYKO3fO3BLR9H5HgeTlaB2aXNuDgRAQposNZ0FEK/VkWj+DQuqjuj9nYo57GbfMfhWr/dTKxTVj3xsbFdThDWtlp7sVI2jguqntwwlmhhexJp1fAYZn92KYkaxGHWLbR0bxLEWVjHXciVW2D12IUZfGWXh5wInoQN1gs1i6NUqgf1uDZhAax5H9G07YySR2fnM9TB5c5apyf7PFCg1kZAnqVHrE= jerome@parrot + EOT - // Terraform will crash without rootfs defined + memory = 512 + swap = 512 + cores = 1 rootfs { storage = "local-lvm" size = "8G" } - network { name = "eth0" bridge = "vmbr0" - ip = "dhcp" + ip = "192.168.2.${var.prem_id + count.index}/24" + gw = "192.168.2.254" + } + + provisioner "remote-exec" { + connection { + type = "ssh" + host = "192.168.2.${var.prem_id + count.index}" + user = "root" + private_key = file("~/.ssh/id_rsa") + timeout = "5m" + } + inline = [ + "touch terraform_ok" + ] } + + provisioner "local-exec" { + command = "./local_script 192.168.2.${var.prem_id + count.index}" + } + } + diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..5b7a68a --- /dev/null +++ b/variables.tf @@ -0,0 +1,27 @@ +# définition des variables + +variable "nb" { + description = "nombre de machines" + type = number + default = 2 +} + +variable "prenom" { + description = "prenom stagiaire" + type = list(any) + default = ["alice", "bob"] +} + +variable "template" { + description = "template container" + type = string + #default = "local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst" + default = "local:vztmpl/rockylinux-9-custom_amd64.tar.gz" +} + +variable "prem_id" { + description = "première vmid" + type = number + default = 104 +} + -- cgit v1.2.3