From e9671cbe81cd22d335409109cbddae896393e6e5 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 18 Feb 2022 15:50:37 +0100 Subject: [PATCH] Always run ansible --- .../create-resources/apache-install.yml | 35 +++++++++++++ infrastructure/create-resources/hcloud.tf | 49 +++++++++++++++++++ .../create-resources/{main.tf => provider.tf} | 15 ++---- 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 infrastructure/create-resources/apache-install.yml create mode 100644 infrastructure/create-resources/hcloud.tf rename infrastructure/create-resources/{main.tf => provider.tf} (61%) diff --git a/infrastructure/create-resources/apache-install.yml b/infrastructure/create-resources/apache-install.yml new file mode 100644 index 0000000..21ec192 --- /dev/null +++ b/infrastructure/create-resources/apache-install.yml @@ -0,0 +1,35 @@ +- become: yes + hosts: all + name: apache-install + tasks: + - name: Add the user 'kjuulh' and add it to 'sudo' + user: + name: kjuulh + group: sudo + - name: + authorized_key: + user: kjuulh + state: present + key: "{{ lookup('file', pub_key) }}" + - name: Wait for apt to unlock + become: yes + shell: while sudo fuser /var/lib/dpkg/lock >/dev/null >2&1; do sleep 5; done; + + - name: Install apache2 + apt: + name: apache2 + update_cache: yes + state: latest + + - name: enable mod_rewrite + apache2_module: + name: rewrite + state: present + notify: + - Restart apache2 + + handlers: + - name: Restart apache2 + service: + name: apache2 + state: restarted diff --git a/infrastructure/create-resources/hcloud.tf b/infrastructure/create-resources/hcloud.tf new file mode 100644 index 0000000..bb56dd1 --- /dev/null +++ b/infrastructure/create-resources/hcloud.tf @@ -0,0 +1,49 @@ + +resource "hcloud_placement_group" "serverctl_master" { + name = "serverctl_master_group" + type = "spread" +} +variable "serverctl_master_count" { + default = 1 +} + +resource "hcloud_server" "serverctl_master" { + count = var.serverctl_master_count + name = "serverctl-master-${count.index}" + image = "debian-11" + server_type = "cx11" + ssh_keys = [ + var.hcloud_serverctl_ssh_key_id + ] + placement_group_id = hcloud_placement_group.serverctl_master.id + + provisioner "remote-exec" { + inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"] + + connection { + host = self.ipv4_address + type = "ssh" + user = "root" + private_key = file(var.pvt_key) + } + } +} + +resource "null_resource" "configure_serverctl_master" { + count = var.serverctl_master_count + + provisioner "local-exec" { + command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -u root -i '${element(hcloud_server.serverctl_master.*.ipv4_address, 0)},' --private-key ${var.pvt_key} -e 'pub_key=${var.pub_key}' apache-install.yml" + } + + triggers = { + always_run = timestamp() + } +} + +output "master_ipv4_addresses" { + value = { + for serverctl in hcloud_server.serverctl_master: + serverctl.name => serverctl.ipv4_address + } +} diff --git a/infrastructure/create-resources/main.tf b/infrastructure/create-resources/provider.tf similarity index 61% rename from infrastructure/create-resources/main.tf rename to infrastructure/create-resources/provider.tf index cc595b2..e06523a 100644 --- a/infrastructure/create-resources/main.tf +++ b/infrastructure/create-resources/provider.tf @@ -29,15 +29,8 @@ provider "hcloud" { token = var.hcloud_token } -resource "hcloud_placement_group" "serverctl_master" { - name = "serverctl_master_group" - type = "spread" -} -resource "hcloud_server" "serverctl_master" { - count = 0 - name = "serverctl-master-${count.index}" - image = "debian-11" - server_type = "cx11" - placement_group_id = hcloud_placement_group.serverctl_master.id -} +variable "hcloud_serverctl_ssh_key_id" {} +variable "pvt_key" {} +variable "pub_key" {} +