diff --git a/vm-ansible/Pulumi.yaml b/vm-ansible/Pulumi.yaml index a73633e..f5f7dc9 100644 --- a/vm-ansible/Pulumi.yaml +++ b/vm-ansible/Pulumi.yaml @@ -7,8 +7,8 @@ template: config: libvirt:uri: type: String - default: qemu:///system - description: QEMU service path + default: "qemu:///system" + description: "QEMU service path" hostname: type: String default: ${PROJECT} @@ -16,19 +16,19 @@ template: image: type: String default: bookworm - description: Image name + description: Image name (see additional_configs.py) vcpu: type: Integer default: 1 - description: Virtual Machine vCPU number + description: Number of vCPU of the VM ram: type: Integer default: 2048 - description: RAM (MB) + description: MBs of RAM of the VM disksize: type: Integer default: 20 - description: Disk space (GB) + description: GBs of disk space of the VM poolpath: type: String default: /var/tmp/libvirt/pulumi @@ -36,12 +36,12 @@ template: username: type: String default: syntaxerrormmm - description: Virtual machine username + description: Username to create in the cloud-init phase password: type: String default: cicciopasticcio - description: Virtual Machine password + description: Password of the username in the cloud-init phase playbooks: type: String default: '' - description: Other playbooks (multi with commas) + description: "Additional playbooks to implement, separate by commas." diff --git a/vm-ansible/__main__.py b/vm-ansible/__main__.py index c4ca2bd..ea7ccb7 100644 --- a/vm-ansible/__main__.py +++ b/vm-ansible/__main__.py @@ -6,7 +6,7 @@ import pulumi_command as command import pulumiverse_time as time import additional_configs as add from jinja2 import Template -import ansible +from passlib.hash import sha512_crypt config = pulumi.Config() @@ -66,8 +66,27 @@ 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() +# Creating the inventory file +inventory = command.local.Command("a-inventory", + create = vm.network_interfaces[0]['addresses'][0].apply( + lambda ipaddr: f"echo '{env['name']} ansible_host={ipaddr} ansible_user={env['username']}' >./inventory" + ), + delete = "rm -f ./inventory", + opts = pulumi.ResourceOptions(depends_on = [ wait_time ]) +) + +deploy = command.local.Command('ap deploy', + create = 'ansible-playbook deploy.yml', + delete = 'rm -f ./ansible.log', + opts = pulumi.ResourceOptions(depends_on = [ inventory ]) +) + +# Running additional playbooks +for playbook in sorted(config.get('playbooks').split(',')): + execute_ansible = command.local.Command(f"ap {playbook.split('.')[0]}", + create = f"ansible-playbook {playbook}", + delete = "rm -f ./ansible.log", + opts = pulumi.ResourceOptions(depends_on = [ deploy ]) + ) pulumi.export("ip", vm.network_interfaces[0]['addresses'][0]) diff --git a/vm-ansible/ansible.py b/vm-ansible/ansible.py deleted file mode 100644 index 1af8c92..0000000 --- a/vm-ansible/ansible.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python3 -# encoding: utf-8 - -import pulumi -from pulumi_command.local import Command - -class AnsibleDeployment(): - - def __init__(self, virtual_machine, environment, additionals = None, deploy = None): - """Creates an ansible deployment and attaches resources.""" - - self.inventory = 'inventory' - self.virtual_machine = virtual_machine - self.environment = environment - self.deploy = str(deploy) or 'deploy.yml' - self.additionals = str(additionals) or '' - - def create_inventory(self, dependencies = []): - """Returns a basic inventory from a virtual_machine object.""" - deps = [] - if len(dependencies) == 0: - deps.append(self.virtual_machine) - else: - deps.extend(dependencies) - - self.inventory = Command("a inventory", - create = self.virtual_machine.network_interfaces[0]['addresses'][0].apply( - lambda ipaddr: f"echo '{self.environment['name']} ansible_host={ipaddr} ansible_user={self.environment['username']}' >./inventory" - ), - delete = "rm -f ./inventory", - opts = pulumi.ResourceOptions(depends_on = dependencies) - ) - - def run_playbooks(self): - """Execute the rest of the playbooks.""" - - self.deploy_resource = Command("ap deploy", - create = "ansible-playbook deploy.yml", - delete = "rm -f ansible.log", - opts = pulumi.ResourceOptions(depends_on = [ self.inventory ]) - ) - - # Running additionals if present - if len(self.additionals) != 0: - - for playbook in sorted(self.additionals.split(',')): - name = playbook.split('.')[0] - Command(f"ap {name}", - create = f"ansible-playbook {playbook}", - delete = "rm -f ansible.log", - opts = pulumi.ResourceOptions(depends_on = [ self.inventory, self.deploy_resource ]) - ) - - - - - - -