Compare commits

..

No commits in common. "b113990fda85e8b6a736cd92003db501cd431945" and "d1af10892d2b48badd1ee9b550417ffec30f3c77" have entirely different histories.

5 changed files with 32 additions and 82 deletions

View File

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

@ -1,11 +0,0 @@
#!/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"
]

View File

@ -1,52 +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.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 ])
)

View File

@ -1,7 +1,6 @@
--- ---
- hosts: all - hosts: all
roles: roles:
- fixhostname
- base - base
become: yes become: yes
become_user: root become_user: root

View File

@ -0,0 +1,6 @@
---
- hosts: all
roles:
- fixhostname
become: yes
become_user: root