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}"))
# 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 = [ 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 = 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 ])
# )
# Outputs
pulumi.export("ipv4_addresses", vm.ipv4_addresses)

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# encoding: utf-8
from typing import List, Optional
from typing import List
from proxmoxer import ProxmoxAPI
import time
@ -20,27 +20,32 @@ class PveWrapper:
self.instance = self.conn.nodes(nodename)
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:
try:
netints = self.instance.qemu(int(vmid)).agent.get("network-get-interfaces")
except:
time.sleep(10)
time.sleep(5)
continue
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
filtered = [ x for x in netints['result'] if x['name'] != 'lo' ]
retval = []
for interface in filtered:
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.
ipv4_address = ipv4_addresses[0]
# 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]
retval.append({ "name": interface['name'],
"ipv4_address": ipv4_address })
retval.append({
"name": interface['name'],
"address": ipv4_address
})
return retval