Tentativo di cambio per pvm-ansible.

This commit is contained in:
Emiliano Vavassori 2024-12-26 21:46:45 +01:00
parent b113990fda
commit 05af88acd9
5 changed files with 45 additions and 34 deletions

View File

@ -8,11 +8,11 @@ template:
config:
pveHostname:
type: String
default: pve.vavassori.lcl
default: vhost.vavassori.lcl
description: PVE hostname
pveNodeName:
type: String
default: pve
default: vhost
description: Node of the Proxmox VE to which write the VM.
pveVerifySSL:
type: Number
@ -28,7 +28,7 @@ template:
description: Authentication token value for PVE Node
pveDefaultStorage:
type: String
default: local-lvm
default: local-zfs
description: Default storage in which put the VMs
hostname:
type: String

View File

@ -18,17 +18,18 @@ verify = True if config.get_int("pveVerifySSL") == 1 else False
pvedefaultstorage = config.require("pveDefaultStorage")
vm_name: str = config.require("hostname")
vm_username: str = config.require("cloudUsername")
tokenvalue = config.require_secret("pveTokenValue")
provider = proxmox.Provider(pvehostname,
endpoint=pveURL,
insecure=not verify,
api_token=config.require_secret("pveTokenValue").apply(lambda x: f"{username}!{tokenname}={x}")
api_token=tokenvalue.apply(lambda x: f"{username}!{tokenname}={x}")
)
pve = PveWrapper(pvehostname,
username=username,
token_name=tokenname,
token_value=config.require_secret("pveTokenValue").apply(lambda v: f"{v}"),
token_value=tokenvalue.apply(lambda v: f"{v}"),
nodename=pvenodename,
verify_ssl=verify
)
@ -37,10 +38,10 @@ vm = proxmox.vm.VirtualMachine(vm_name,
name = vm_name,
node_name = pvenodename,
agent = proxmox.vm.VirtualMachineAgentArgs(
enabled = True,
trim = True,
type = "virtio"
),
enabled=True,
type="virtio",
timeout="2m",
),
cpu = proxmox.vm.VirtualMachineCpuArgs(
cores = config.get_int("vmSockets"),
sockets = config.get_int("vmCores")
@ -75,7 +76,8 @@ vm = proxmox.vm.VirtualMachine(vm_name,
username = vm_username,
password = config.require_secret("cloudPassword"),
keys = additional_config.sshkeys
)
),
vendor_data_file_id = "cloud-init/qemu-guest-agent_deb.yml"
),
opts = pulumi.ResourceOptions(
provider = provider,
@ -83,27 +85,27 @@ vm = proxmox.vm.VirtualMachine(vm_name,
)
)
ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}"))
ipv4_addresses = vm.id.apply(lambda id: pve.ipv4_addresses(f"{id}"))
## Creating the inventory file
#inventory = command.local.Command("a-inventory",
# create = vm.ipv4_addresses.apply(lambda ipaddr:
# f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory"
# ),
# delete = "rm -f ./inventory",
# opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ])
#)
#
## Applying the command
#for playbook in additional_config.playbooks:
# shortname: str = playbook.split(".")[0]
# command.local.Command(f"ap-{shortname}",
# create = vm.ipv4_addresses.apply(
# lambda run: f"ansible-playbook {run[0]['ipv4_address']}"
# ),
# delete = "rm -f ./ansible.log",
# opts = pulumi.ResourceOptions(depends_on = [ inventory ])
# )
# Creating the inventory file
inventory = command.local.Command("a-inventory",
create = ipv4_addresses.apply(lambda ipaddr:
f"echo '{vm_name} ansible_host={ipaddr[0]['address']} ansible_user={vm_username}' >./inventory"
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ])
)
# Applying the command
for playbook in additional_config.playbooks:
shortname: str = playbook.split(".")[0]
command.local.Command(f"ap-{shortname}",
create = vm.ipv4_addresses.apply(
lambda run: f"ansible-playbook {run[0]['ipv4_address']}"
),
delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
)
# Outputs
pulumi.export("ipv4_addresses", vm.ipv4_addresses)

View File

@ -14,8 +14,12 @@ playbooks = [
]
os_to_template = {
"tpl-ci-debian12": 808,
"debian12": 808,
"bookworm": 808
"tpl-ci-debian12": 901,
"debian12": 901,
"bookworm": 901,
"tpl-ci-ubuntu2404": 904,
"ubuntu2404": 904,
"tpl-ci-rocky9": 902,
"rocky9": 902
}

View File

@ -0,0 +1,5 @@
#cloud-config
runcmd:
- apt update
- apt install -y qemu-guest-agent
- systemctl start qemu-guest-agent

View File

@ -7,7 +7,7 @@ import time
class PveWrapper:
def __init__(self, hostname: str, username: str, token_name: str, token_value: str, nodename: str, verify_ssl=False) -> None:
def __init__(self, hostname, username, token_name, token_value, nodename, verify_ssl=False) -> None:
"""Wrapper for Proxmox API query."""
self.conn = ProxmoxAPI(