pulumi-templates/vm-ansible/__main__.py

74 lines
2.0 KiB
Python
Raw Normal View History

2022-08-20 00:45:47 +02:00
"""A Pulumi-based infrastructure with libvirt and Python"""
import pulumi
import pulumi_libvirt as lv
import pulumi_command as command
import pulumiverse_time as time
import additional_configs as add
from jinja2 import Template
import ansible
2022-08-20 00:45:47 +02:00
config = pulumi.Config()
env = {
'username': config.get('username'),
'password': config.get('password'),
'sshkeys': add.SSH_KEYS,
'name': config.get('hostname'),
}
pool = lv.Pool("pool",
2022-08-20 00:45:47 +02:00
type = "dir",
path = config.get('poolpath')
2022-08-20 00:45:47 +02:00
)
baseimg = lv.Volume("base-image",
pool = pool.name,
source = add.CLOUD_IMAGES[config.get('image')]
2022-08-20 00:45:47 +02:00
)
volume = lv.Volume("disk",
base_volume_id = baseimg.id,
pool = pool.name,
size = int(config.get('disksize')) * 1024 ** 3
2022-08-20 00:45:47 +02:00
)
# Stream configuration files
cloudinit = lv.CloudInitDisk("cloud-init",
meta_data = Template(open("./templates/meta-data.jinja", "r").read()).render(env),
user_data = Template(open("./templates/user-data.jinja", "r").read()).render(env),
network_config = open("./templates/network-config", "r").read()
2022-08-20 00:45:47 +02:00
)
vm = lv.Domain(config.get('hostname'),
2022-08-20 00:45:47 +02:00
boot_devices = [ lv.DomainBootDeviceArgs(
devs = [ "hd", "cdrom" ]
) ],
cloudinit = cloudinit.id,
vcpu = config.get('vcpu'),
disks = [ lv.DomainDiskArgs(volume_id = volume.id, scsi=True) ],
memory = int(config.get('ram')),
2022-08-20 00:45:47 +02:00
network_interfaces = [ lv.DomainNetworkInterfaceArgs(
network_name = "default",
wait_for_lease = True
) ],
consoles = [ lv.DomainConsoleArgs(
type = "pty",
target_port = 0,
target_type = "serial"
) ],
opts = pulumi.ResourceOptions(
custom_timeouts = pulumi.CustomTimeouts(create = "3m")
)
)
wait_time = time.Sleep("wait_time", create_duration="30s",
opts = pulumi.ResourceOptions(depends_on = [ vm ])
)
adeploy = ansible.AnsibleDeployment(vm, env, config.get('playbooks'))
adeploy.create_inventory([ wait_time ])
adeploy.run_playbooks()
2022-08-20 00:45:47 +02:00
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])