Tentativo di fix della chiamata a funzione per indirizzi IP. Rimosso codice per Ansible per testing.

This commit is contained in:
Emiliano Vavassori 2024-09-09 17:23:10 +02:00
parent 529dfe9be2
commit 0cbc891cfb
2 changed files with 36 additions and 31 deletions

View File

@ -85,25 +85,25 @@ vm = proxmox.vm.VirtualMachine(vm_name,
ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}")) ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}"))
# Creating the inventory file ## Creating the inventory file
inventory = command.local.Command("a-inventory", #inventory = command.local.Command("a-inventory",
create = vm.ipv4_addresses.apply(lambda ipaddr: # create = vm.ipv4_addresses.apply(lambda ipaddr:
f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory" # f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory"
), # ),
delete = "rm -f ./inventory", # delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ ipv4_addresses ]) # opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ])
) #)
#
# Applying the command ## Applying the command
for playbook in additional_config.playbooks: #for playbook in additional_config.playbooks:
shortname: str = playbook.split(".")[0] # shortname: str = playbook.split(".")[0]
command.local.Command(f"ap-{shortname}", # command.local.Command(f"ap-{shortname}",
create = vm.ipv4_addresses.apply( # create = vm.ipv4_addresses.apply(
lambda run: f"ansible-playbook {run[0]['ipv4_address']}" # lambda run: f"ansible-playbook {run[0]['ipv4_address']}"
), # ),
delete = "rm -f ./ansible.log", # delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ]) # opts = pulumi.ResourceOptions(depends_on = [ inventory ])
) # )
# Outputs # Outputs
pulumi.export("ipv4_addresses", vm.ipv4_addresses) pulumi.export("ipv4_addresses", vm.ipv4_addresses)

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
from typing import List, Optional from typing import List
from proxmoxer import ProxmoxAPI from proxmoxer import ProxmoxAPI
import time import time
@ -20,27 +20,32 @@ class PveWrapper:
self.instance = self.conn.nodes(nodename) self.instance = self.conn.nodes(nodename)
def ipv4_addresses(self, vmid: str) -> List[str]: def ipv4_addresses(self, vmid: str) -> List[str]:
"""Returns the list of ipv4_addresses.""" """Returns the list of ipv4_addresses of the VM."""
netints = {}
while True: while True:
try: try:
netints = self.instance.qemu(int(vmid)).agent.get("network-get-interfaces") netints = self.instance.qemu(int(vmid)).agent.get("network-get-interfaces")
except: except:
time.sleep(10) time.sleep(5)
continue continue
else: else:
# When QEMU agent is running, return the array. # When QEMU agent is running, all the needed informations are
# retrieved. No need to check each possible outcome.
# All non-lo interfaces # All non-lo interfaces
filtered = [ x for x in netints['result'] if x['name'] != 'lo' ] filtered = [ x for x in netints['result'] if x['name'] != 'lo' ]
retval = [] retval = []
for interface in filtered: for interface in filtered:
ipv4_addresses = [ x['ip-address'] for x in interface['ip-addresses'] if x['ip-address-type'] == 'ipv4' ] ipv4_addresses = [ x['ip-address'] for x in interface['ip-addresses'] if x['ip-address-type'] == 'ipv4' ]
# We assume a new machine has just been spawned, so it has just 1 ipv4 address. # We assume a new machine has just been spawned, so it has just 1 ipv4 address per interface (if any).
ipv4_address = ''
if len(ipv4_addresses) >= 1:
ipv4_address = ipv4_addresses[0] ipv4_address = ipv4_addresses[0]
retval.append({ "name": interface['name'], retval.append({
"ipv4_address": ipv4_address }) "name": interface['name'],
"address": ipv4_address
})
return retval return retval