From 26e4cb28f2a09671848273df7086a115afcc3ca6 Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Sun, 8 Sep 2024 02:39:46 +0200 Subject: [PATCH] Ancora qualche avanzamento su template linode-ansible. --- linode-ansible/Pulumi.yaml | 26 ++++++++++++++++++++--- linode-ansible/__main__.py | 39 ++++++++++++++++++---------------- linode-ansible/fixhostname.yml | 6 ++++++ linode-ansible/input.yaml | 20 +++++++---------- 4 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 linode-ansible/fixhostname.yml diff --git a/linode-ansible/Pulumi.yaml b/linode-ansible/Pulumi.yaml index bba8b31..6afe23e 100644 --- a/linode-ansible/Pulumi.yaml +++ b/linode-ansible/Pulumi.yaml @@ -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 diff --git a/linode-ansible/__main__.py b/linode-ansible/__main__.py index 6f48d62..a84aea2 100644 --- a/linode-ansible/__main__.py +++ b/linode-ansible/__main__.py @@ -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,18 +36,20 @@ 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 ]) ) -# Applying the command -execute_ansible = command.local.Command("a-deploy", - create = f"ansible-playbook {config.vm.playbook}", - delete = "rm -f ./ansible.log", - opts = pulumi.ResourceOptions(depends_on = [ inventory ]) -) +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 ]) + ) # Export the Instance label of the instance pulumi.export('ip_address', instance.ip_address) diff --git a/linode-ansible/fixhostname.yml b/linode-ansible/fixhostname.yml new file mode 100644 index 0000000..1e33c1d --- /dev/null +++ b/linode-ansible/fixhostname.yml @@ -0,0 +1,6 @@ +--- +- hosts: all + roles: + - fixhostname + become: yes + become_user: root diff --git a/linode-ansible/input.yaml b/linode-ansible/input.yaml index 662b7d5..594266c 100644 --- a/linode-ansible/input.yaml +++ b/linode-ansible/input.yaml @@ -1,14 +1,10 @@ --- -linode: - type: g6-nanode-1 - region: eu-central - image: linode/debian12 - # Basic VM configuration -vm: - root_password: cicciopasticcio - sshkeys: - - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso" - - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps" - - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi" - playbook: deploy.yml +rootpassword: cicciopasticcio +sshkeys: + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso" + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps" + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi" +playbooks: + - fixhostname.yml + - deploy.yml