From 5cc121984b649643cfcae9a53fbca64b5f2785e8 Mon Sep 17 00:00:00 2001 From: guppy Date: Sun, 30 Apr 2023 15:14:06 +0200 Subject: [PATCH] Better version --- ansible/cloud-config-gen.sh | 14 ++++++++++++++ ansible/cloud-init-conf.yml | 9 --------- ansible/create-vm-template.yaml | 17 ++++++++++++++++- terraform/main.tf | 29 +++++++++++++++++++++++------ terraform/variables.tf | 23 +++++++++++++++++++++-- 5 files changed, 74 insertions(+), 18 deletions(-) create mode 100755 ansible/cloud-config-gen.sh delete mode 100644 ansible/cloud-init-conf.yml diff --git a/ansible/cloud-config-gen.sh b/ansible/cloud-config-gen.sh new file mode 100755 index 0000000..19e6600 --- /dev/null +++ b/ansible/cloud-config-gen.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# +# Script to generate a cloud-config file with a custom user and the script user SSH public key as authorized key +# +cat << EOF > cloud-init-conf.yml +#cloud-config +users: + - name: ansuser + ssh-authorized-keys: + - $(cat ~/.ssh/id_rsa.pub) + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + groups: sudo + shell: /bin/bash +EOF diff --git a/ansible/cloud-init-conf.yml b/ansible/cloud-init-conf.yml deleted file mode 100644 index b747d50..0000000 --- a/ansible/cloud-init-conf.yml +++ /dev/null @@ -1,9 +0,0 @@ -#cloud-config -users: - - default - - name: ansuser - ssh-authorized-keys: - - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDeDXn6rKQ/E6ND36C9r+04DS78C3d9hpSzcvBbBvCZ7kPEUFVrV0MiLZbBCAaaunIFsQ9hfq2ZlrlDmYLWhaxdtS8VEckJiZarvQ32aLEwFBaW7NwF1i5qyHaz7pL1NPQKHbQ3s7pBrUKotD3yX1yZIXA9RyCCpru1sWxhDRROWgu20FGF6D6Mv3gw9qORdEnrmtqz4ovYVHj3MuzHRVGttDvhrAoXDa66dLrpYaYdLSAjQqtuW0TO+ZTH3YLE7thwmh/QdHuDSD1Bs1r5yYli/CiljyxD2H1Q+wcivYEuc4Pk/B06uKFo+9wLBWVHHTrzsforGY0FQiUO+dX5qZQMhPotL9BUDkJcUNBoUycyuuHnj+REtaMlJChLPXhXGY6l7uczNfOPXCj+AlbK9KTqeAdLH81pJgD0F2F+j+pBtHu5oxQzcAkD+k1t4c+ndWNMO+MADCUOmxFijDr+S3F1aIUL1ixovQ9w6+RxkuPzGCGKnPM/9z+yhZyvZplMk1Bd8oAOqMCZqAfTDzuVl1Te/xq6PoArJ5mtLBoATrFHRsg2J07tJf3USjFIzp20EZiEdePs/slQ8rqOuYqCDKlMXZY0YBObmtVOIMaWKHzqkmO59B+aFDbLgHvwdSaSoqvrTHD4E1F2G3Oz08bo+QK4//4zKdjn1TMKzzg/pQ/idQ== guppy@D11-TP - sudo: ['ALL=(ALL) NOPASSWD:ALL'] - groups: sudo - shell: /bin/bash diff --git a/ansible/create-vm-template.yaml b/ansible/create-vm-template.yaml index a4b4778..50568bb 100644 --- a/ansible/create-vm-template.yaml +++ b/ansible/create-vm-template.yaml @@ -10,7 +10,7 @@ template_id: 1001 template_name: debian-11-cloudinit-template template_memory: 4096 - template_bridge: vmbr0 + template_bridge: vmbr100 tasks: - name: destroy template command: "qm destroy {{ vm.template_id }}" @@ -23,6 +23,17 @@ dest: "{{ vm.cloud_image_path }}" mode: 0700 + - name: Install Cloud-init + apt: + name: + - cloud-init + - libguestfs-tools + become: yes + + - name: Install QEMU agent on img + command: "virt-customize -a {{ vm.cloud_image_path }} --install qemu-guest-agent" + become: yes + - name : Copy cloud-init-config file copy: src: cloud-init-conf.yml @@ -54,6 +65,10 @@ command: "qm set {{ vm.template_id }} --serial0 socket --vga serial0" become: yes + - name: enable qemu agent + command: "qm set {{ vm.template_id }} --agent 1" + become: yes + - name: add cloud-init-config command: 'qm set {{ vm.template_id }} --cicustom "user=local:snippets/cloud-init-conf.yml"' become: yes diff --git a/terraform/main.tf b/terraform/main.tf index 752d189..5ba8317 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -12,22 +12,24 @@ resource "proxmox_vm_qemu" "control_plane" { memory = 2048 scsihw = "virtio-scsi-pci" bootdisk = "scsi0" + agent = 1 disk { size = "20G" type = "scsi" storage = "local-lvm" - iothread = 1 + iothread = 0 } network { model = "virtio" - bridge = "vmbr0" + bridge = "${var.vm_vmbr}" + tag = "${var.vm_vlan}" } # cloud-init settings # adjust the ip and gateway addresses as needed - ipconfig0 = "ip=192.168.1.1${count.index}/24,gw=192.168.1.254" + ipconfig0 = "ip=${var.vm_plane_control_network}${count.index}/24,gw=${var.vm_gateway}" } resource "proxmox_vm_qemu" "worker_nodes" { @@ -44,20 +46,35 @@ resource "proxmox_vm_qemu" "worker_nodes" { memory = 2048 scsihw = "virtio-scsi-pci" bootdisk = "scsi0" + agent = 1 disk { size = "20G" type = "scsi" storage = "local-lvm" - iothread = 1 + iothread = 0 } network { model = "virtio" - bridge = "vmbr0" + bridge = "${var.vm_vmbr}" + tag = "${var.vm_vlan}" } # cloud-init settings # adjust the ip and gateway addresses as needed - ipconfig0 = "ip=192.168.1.2${count.index}/24,gw=192.168.1.254" + ipconfig0 = "ip=${var.vm_worker_nodes_network}${count.index}/24,gw=${var.vm_gateway}" +} + +# Generate inventory file +resource "local_file" "inventory" { + filename = "hosts.ini" + content = <