Cambiata logica di setup delle configurazioni della VM: in uso template per file cloud-init.

This commit is contained in:
Emiliano Vavassori 2022-08-20 01:29:44 +02:00
parent b5bce9cb17
commit 6267a7e843
8 changed files with 69 additions and 43 deletions

View File

@ -1,10 +1,7 @@
# Quick configuration #
To quickly set up your pulumi deployment, configure the following settings:
Check out the file `input.yml` for a quick setup for a new machine.
Also, don't forget to setup the password for your account on your ProxmoxVE
server configuring `pulumi`:
$ pulumi config set libvirt:uri --plaintext "qemu:///system"
$ pulumi config set image_url --plaintext "https://cdimage.debian.org/images/cloud/bullseye-backports/20220711-1073/debian-11-backports-generic-amd64-20220711-1073.qcow2"
$ pulumi config set disksize --plaintext 20
$ pulumi config set memory --plaintext 2048
$ pulumi config set ansible_playbook --plaintext deploy.yml

View File

@ -3,29 +3,38 @@
import pulumi
import pulumi_libvirt as lv
import pulumi_command as command
import yaml
from dotmap import DotMap
from jinja2 import Template
from passlib import sha512_crypt
conf = DotMap(yaml.safe_load(open("./input.yaml", "r").read()))
# Replacing readed password with its own counterpart.
conf.password = sha512_crypt.hash(conf.password, rounds=4096)
config = pulumi.Config()
pool = lv.Pool("default",
pool = lv.Pool("pool",
type = "dir",
path = "/var/tmp/libvirt/pulumi"
path = conf.poolpath
)
baseimg = lv.Volume("base-image",
pool = pool.name,
source = config.require("image_url")
source = conf.image_url
#source = "https://cdimage.debian.org/images/cloud/bullseye-backports/20220711-1073/debian-11-backports-generic-amd64-20220711-1073.qcow2"
)
volume = lv.Volume("disk",
base_volume_id = baseimg.id,
pool = pool.name,
size = int(config.require("disksize")) * 1024 ** 3
size = int(conf.disksize) * 1024 ** 3
)
# Stream configuration files
userdata = open("./cloud-init/user-data", "r").read()
metadata = open("./cloud-init/meta-data", "r").read()
userdata = Template(open("./cloud-init/user-data.jinja", "r").read()).render(conf)
metadata = Template(open("./cloud-init/meta-data.jinja", "r").read()).render(conf)
networkconfig = open("./cloud-init/network-config", "r").read()
cloudinit = lv.CloudInitDisk("cloud-init",
@ -34,13 +43,13 @@ cloudinit = lv.CloudInitDisk("cloud-init",
network_config = networkconfig
)
vm = lv.Domain("debian11",
vm = lv.Domain(vm.name,
boot_devices = [ lv.DomainBootDeviceArgs(
devs = [ "hd", "cdrom" ]
) ],
cloudinit = cloudinit.id,
disks = [ lv.DomainDiskArgs(volume_id = volume.id) ],
memory = int(config.require("memory")),
memory = int(conf.ram),
network_interfaces = [ lv.DomainNetworkInterfaceArgs(
network_name = "default",
wait_for_lease = True
@ -58,16 +67,15 @@ vm = lv.Domain("debian11",
# Creating the inventory file
inventory = command.local.Command("a-inventory",
create = vm.network_interfaces[0]['addresses'][0].apply(
lambda ipaddr: f"echo '{vm_name} ansible_host={ipaddr} ansible_user=syntaxerrormm' >./inventory"
lambda ipaddr: f"echo '{conf.name} ansible_host={ipaddr} ansible_user={conf.username}' >./inventory"
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ vm ])
)
# Applying the command
ansible_playbook = config.require("ansible_playbook")
execute_ansible = command.local.Command("a-deploy",
create = f"ansible-playbook {ansible_playbook}",
create = f"ansible-playbook {conf.ansible_playbook}",
delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
)

View File

@ -1,2 +0,0 @@
instance-id: debian11
local-hostname: debian11

View File

@ -0,0 +1,2 @@
instance-id: {{ conf.name }}
local-hostname: {{ conf.name }}

View File

@ -1,24 +0,0 @@
#cloud-config
resize_rootfs: true
users:
- name: syntaxerrormmm
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
shell: /bin/bash
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP/hn/0xn6DRS2B0paFzDQRakupgTQQ5hitQhqOfWcqz syntaxerrormmm@microbo
hashed_passwd: $6$rounds=4096$4ZUeoch.EwyR/TLb$.71i1otUQpw2eMlrjcP3H4XxsnLlVbIQxagFDGviOvCaIz7ONa.tTZPO09YL7hiAh4vLaTcGD43VoAM6tEYjx/
ssh_pwauth: true
disable_root: false
chpasswd:
expire: false
list: |
root:$6$rounds=4096$4ZUeoch.EwyR/TLb$.71i1otUQpw2eMlrjcP3H4XxsnLlVbIQxagFDGviOvCaIz7ONa.tTZPO09YL7hiAh4vLaTcGD43VoAM6tEYjx/
syntaxerrormmm:$6$rounds=4096$4ZUeoch.EwyR/TLb$.71i1otUQpw2eMlrjcP3H4XxsnLlVbIQxagFDGviOvCaIz7ONa.tTZPO09YL7hiAh4vLaTcGD43VoAM6tEYjx/
#packages:
# - qemu-guest-agent
#power_state:
# delay: now
# mode: reboot

View File

@ -0,0 +1,24 @@
#cloud-config
resize_rootfs: true
users:
- name: {{ conf.username }}
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
shell: /bin/bash
ssh_authorized_keys:
{% for key in conf.sshkeys %}
- {{ item }}
{% endfor %}
hashed_passwd: {{ conf.password }}
ssh_pwauth: true
disable_root: false
chpasswd:
expire: false
list: |
root:{{ conf.password }}
syntaxerrormmm:{{ conf.password }}
#packages:
# - qemu-guest-agent
#power_state:
# delay: now
# mode: reboot

View File

@ -0,0 +1,18 @@
---
# Basic VM configuration
name: virtual-machine
vcpu: 1
ram: 2048
disksize: 20
username: syntaxerrormmm
password: cicciopasticcio
sshkeys:
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP/hn/0xn6DRS2B0paFzDQRakupgTQQ5hitQhqOfWcqz syntaxerrormmm@microbo"
image_url: "https://cdimage.debian.org/images/cloud/bullseye-backports/20220711-1073/debian-11-backports-generic-amd64-20220711-1073.qcow2"
poolpath: /var/tmp/libvirt/pulumi
# Ansible configuration
ansible_playbook: deploy.yml

View File

@ -1,3 +1,6 @@
pulumi>=3.0.0,<4.0.0
pulumi_libvirt>=0.3.0
pulumi-command>=0.4.1
Jinja2>=3.1.2
dotmap>=1.3.30
passlib>=1.7.4