Ancora qualche avanzamento su template linode-ansible.

This commit is contained in:
Emiliano Vavassori 2024-09-08 02:39:46 +02:00
parent 7f993ce140
commit 26e4cb28f2
4 changed files with 58 additions and 33 deletions

View File

@ -1,13 +1,33 @@
name: ${PROJECT} name: ${PROJECT}
description: ${DESCRIPTION} description: ${DESCRIPTION}
runtime: python runtime:
name: python
options:
toolchain: pip
virtualenv: venv
template: template:
description: A Pulumi deployment with Linode cloud and Python, enhanced with Ansible. description: A Pulumi deployment with Linode cloud and Python, enhanced with Ansible.
config: config:
name: hostname:
type: String type: String
default: ${PROJECT} default: ${PROJECT}
description: Linode name description: Hostname
domainname:
type: String
default: vavassori.org
description: Domain name
linodeType:
type: String
default: g6-nanode-1
description: Linode instance type
linodeRegion:
type: String
default: eu-central
description: Linode region to load your linode on
linodeImage:
type: String
default: linode/debian12
description: Linode Image to deploy
linode:token: linode:token:
type: String type: String
secret: True secret: True

View File

@ -8,24 +8,25 @@ import yaml
from dotmap import DotMap from dotmap import DotMap
config = DotMap(yaml.safe_load(open("input.yaml").read())) config = DotMap(yaml.safe_load(open("input.yaml").read()))
vmname = pulumi.Config().require("name") hostname = pulumi.Config().require("hostname")
domainname = pulumi.Config().require("domainname")
# Create a Linode resource (Linode Instance) # Create a Linode resource (Linode Instance)
instance = pulumi_linode.Instance(vmname, instance = pulumi_linode.Instance(hostname,
type=config.linode.type, type=pulumi.Config().require("linodeType"),
region=config.linode.region, region=pulumi.Config().require("linodeRegion"),
image=config.linode.image, image=pulumi.Config().require("linodeImage"),
label=vmname, label=hostname,
root_pass=config.vm.root_password, root_pass=config.rootpassword,
authorized_keys=config.vm.sshkeys, authorized_keys=config.sshkeys,
opts=pulumi.ResourceOptions( opts=pulumi.ResourceOptions(
custom_timeouts=pulumi.CustomTimeouts(create="2m") custom_timeouts=pulumi.CustomTimeouts(create="2m")
) )
) )
zone = cloudflare.get_zone(name="vavassori.org") zone = cloudflare.get_zone(name=domainname)
dnsrecord = cloudflare.Record(vmname, dnsrecord = cloudflare.Record(hostname,
name=vmname, name=hostname,
zone_id=zone.id, zone_id=zone.id,
type="A", type="A",
content=instance.ip_address, content=instance.ip_address,
@ -35,18 +36,20 @@ dnsrecord = cloudflare.Record(vmname,
# Creating the inventory file # Creating the inventory file
inventory = command.local.Command("a-inventory", inventory = command.local.Command("a-inventory",
create = instance.ip_address.apply( create = instance.ip_address.apply(
lambda ipaddr: f"echo '{vmname} ansible_host={ipaddr} ansible_user=root' >./inventory" lambda ipaddr: f"echo '{hostname} ansible_host={ipaddr} ansible_user=root hostname={hostname}.{domainname}' >./inventory"
), ),
delete = "rm -f ./inventory", delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ instance ]) opts = pulumi.ResourceOptions(depends_on = [ instance ])
) )
# Applying the command for playbook in config.playbooks:
execute_ansible = command.local.Command("a-deploy", shortname = playbook.split('.')[0]
create = f"ansible-playbook {config.vm.playbook}", # Applying the command
command.local.Command(f"a-deploy-{shortname}",
create = f"ansible-playbook {playbook}",
delete = "rm -f ./ansible.log", delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ]) 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,6 @@
---
- hosts: all
roles:
- fixhostname
become: yes
become_user: root

View File

@ -1,14 +1,10 @@
--- ---
linode:
type: g6-nanode-1
region: eu-central
image: linode/debian12
# Basic VM configuration # Basic VM configuration
vm: rootpassword: cicciopasticcio
root_password: cicciopasticcio sshkeys:
sshkeys:
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso" - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps" - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi" - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi"
playbook: deploy.yml playbooks:
- fixhostname.yml
- deploy.yml