Aggiunta codice per semplificare.

This commit is contained in:
Emiliano Vavassori 2024-09-21 19:53:58 +02:00
parent 048fb2c3b3
commit b113990fda
3 changed files with 81 additions and 26 deletions

View File

@ -4,21 +4,22 @@ import pulumi
import pulumi_linode import pulumi_linode
import pulumi_command as command import pulumi_command as command
import pulumi_cloudflare as cloudflare import pulumi_cloudflare as cloudflare
import yaml import additional_configs
from dotmap import DotMap import ansible
config = DotMap(yaml.safe_load(open("input.yaml").read())) config = pulumi.Config()
hostname = pulumi.Config().require("hostname")
domainname = pulumi.Config().require("domainname") hostname = config.require("hostname")
domainname = config.require("domainname")
# Create a Linode resource (Linode Instance) # Create a Linode resource (Linode Instance)
instance = pulumi_linode.Instance(hostname, instance = pulumi_linode.Instance(hostname,
type=pulumi.Config().require("linodeType"), type=config.require("linodeType"),
region=pulumi.Config().require("linodeRegion"), region=config.require("linodeRegion"),
image=pulumi.Config().require("linodeImage"), image=config.require("linodeImage"),
label=hostname, label=hostname,
root_pass=config.rootpassword, root_pass=additional_configs.DEFAULT_ROOT_PASSWORD,
authorized_keys=config.sshkeys, authorized_keys=additional_configs.SSH_KEYS,
opts=pulumi.ResourceOptions( opts=pulumi.ResourceOptions(
custom_timeouts=pulumi.CustomTimeouts(create="2m") custom_timeouts=pulumi.CustomTimeouts(create="2m")
) )
@ -33,23 +34,14 @@ dnsrecord = cloudflare.Record(hostname,
comment=f"Pulumi-generated linode" comment=f"Pulumi-generated linode"
) )
# Creating the inventory file environment = {
inventory = command.local.Command("a-inventory", 'name': hostname,
create = instance.ip_address.apply( 'hostname': hostname + '.' + domainname,
lambda ipaddr: f"echo '{hostname} ansible_host={ipaddr} ansible_user=root hostname={hostname}.{domainname}' >./inventory" }
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ instance ])
)
for playbook in config.playbooks: adeploy = ansible.AnsibleDeployment(instance, environment, config.get('playbooks'))
shortname = playbook.split('.')[0] adeploy.create_inventory()
# Applying the command adeploy.run_playbooks()
command.local.Command(f"a-deploy-{shortname}",
create = f"ansible-playbook {playbook}",
delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
)
# Export the Instance label of the instance # Export the Instance label of the instance
pulumi.export('ip_address', instance.ip_address) pulumi.export('ip_address', instance.ip_address)

View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
# encoding: utf-8
DEFAULT_ROOT_PASSWORD = 'cicciopasticcio'
SSH_KEYS = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILH5q/ObtC4VhNT88gebezP/svpvCoQLoZCh4DvUn4xq syntaxerrormmm@taz",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi"
]

52
linode-ansible/ansible.py Normal file
View File

@ -0,0 +1,52 @@
#!/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.ip_address.apply(
lambda ipaddr: f"echo '{self.environment['name']} ansible_host={ipaddr} ansible_user=root hostname={self.environment['hostname']} fqdn={self.environment['hostname']}' >./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 ])
)