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}
description: ${DESCRIPTION}
runtime: python
runtime:
name: python
options:
toolchain: pip
virtualenv: venv
template:
description: A Pulumi deployment with Linode cloud and Python, enhanced with Ansible.
config:
name:
hostname:
type: String
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:
type: String
secret: True

View File

@ -8,24 +8,25 @@ import yaml
from dotmap import DotMap
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)
instance = pulumi_linode.Instance(vmname,
type=config.linode.type,
region=config.linode.region,
image=config.linode.image,
label=vmname,
root_pass=config.vm.root_password,
authorized_keys=config.vm.sshkeys,
instance = pulumi_linode.Instance(hostname,
type=pulumi.Config().require("linodeType"),
region=pulumi.Config().require("linodeRegion"),
image=pulumi.Config().require("linodeImage"),
label=hostname,
root_pass=config.rootpassword,
authorized_keys=config.sshkeys,
opts=pulumi.ResourceOptions(
custom_timeouts=pulumi.CustomTimeouts(create="2m")
)
)
zone = cloudflare.get_zone(name="vavassori.org")
dnsrecord = cloudflare.Record(vmname,
name=vmname,
zone = cloudflare.get_zone(name=domainname)
dnsrecord = cloudflare.Record(hostname,
name=hostname,
zone_id=zone.id,
type="A",
content=instance.ip_address,
@ -35,15 +36,17 @@ dnsrecord = cloudflare.Record(vmname,
# Creating the inventory file
inventory = command.local.Command("a-inventory",
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",
opts = pulumi.ResourceOptions(depends_on = [ instance ])
)
for playbook in config.playbooks:
shortname = playbook.split('.')[0]
# Applying the command
execute_ansible = command.local.Command("a-deploy",
create = f"ansible-playbook {config.vm.playbook}",
command.local.Command(f"a-deploy-{shortname}",
create = f"ansible-playbook {playbook}",
delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
)

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
vm:
root_password: cicciopasticcio
rootpassword: cicciopasticcio
sshkeys:
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps"
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi"
playbook: deploy.yml
playbooks:
- fixhostname.yml
- deploy.yml