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