From 170c5e0730446ef7f61a9f5f84c2bc07cecba3bb Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 19 Feb 2022 18:18:19 +0100 Subject: [PATCH] Add ansible to pipeline --- .drone.yml | 4 ++- .../ansible/inventory/hosts.cfg | 2 ++ .../server-install.yml} | 21 +--------------- infrastructure/create-resources/hcloud.tf | 25 +++++++++++++------ infrastructure/create-resources/provider.tf | 11 ++++---- .../create-resources/templates/hosts.tpl | 4 +++ 6 files changed, 32 insertions(+), 35 deletions(-) create mode 100755 infrastructure/create-resources/ansible/inventory/hosts.cfg rename infrastructure/create-resources/{apache-install.yml => ansible/server-install.yml} (51%) create mode 100644 infrastructure/create-resources/templates/hosts.tpl diff --git a/.drone.yml b/.drone.yml index 553254f..dcfe8e8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -5,7 +5,7 @@ steps: - name: terraform plan image: alpine environment: - HCLOUD_TOKEN: + HCLOUD_TOKEN: from_secret: serverctl_hcloud_token ACCESS_KEY: from_secret: serverctl_access_key @@ -26,3 +26,5 @@ steps: - terraform init -backend-config="access_key=$ACCESS_KEY" -backend-config="secret_key=$SECRET_KEY" - terraform validate - terraform apply -auto-approve -var "hcloud_token=$HCLOUD_TOKEN" -var "pvt_key=../ssh_keys/id_ed25519" -var "pub_key=../ssh_keys/id_ed25519.pub" -var "hcloud_serverctl_ssh_key_id=$HCLOUD_SSH_KEY_ID" + - cd ansible + - ANSIBLE_HOST_KEY_CHECKING=False /usr/bin/ansible-playbook -u root --key-file '../../ssh_keys/id_ed25519' server-install.yml diff --git a/infrastructure/create-resources/ansible/inventory/hosts.cfg b/infrastructure/create-resources/ansible/inventory/hosts.cfg new file mode 100755 index 0000000..f95e58b --- /dev/null +++ b/infrastructure/create-resources/ansible/inventory/hosts.cfg @@ -0,0 +1,2 @@ +[serverctl_master_hosts] +65.21.153.18 diff --git a/infrastructure/create-resources/apache-install.yml b/infrastructure/create-resources/ansible/server-install.yml similarity index 51% rename from infrastructure/create-resources/apache-install.yml rename to infrastructure/create-resources/ansible/server-install.yml index 21ec192..3bc9cf2 100644 --- a/infrastructure/create-resources/apache-install.yml +++ b/infrastructure/create-resources/ansible/server-install.yml @@ -1,6 +1,6 @@ - become: yes hosts: all - name: apache-install + name: server-install tasks: - name: Add the user 'kjuulh' and add it to 'sudo' user: @@ -14,22 +14,3 @@ - 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 index a818662..391d0c0 100644 --- a/infrastructure/create-resources/hcloud.tf +++ b/infrastructure/create-resources/hcloud.tf @@ -8,9 +8,9 @@ variable "serverctl_master_count" { } resource "hcloud_server" "serverctl_master" { - count = var.serverctl_master_count - name = "serverctl-master-${count.index}" - image = "debian-11" + count = var.serverctl_master_count + name = "serverctl-master-${count.index}" + image = "debian-11" server_type = "cx11" ssh_keys = [ var.hcloud_serverctl_ssh_key_id @@ -21,9 +21,9 @@ resource "hcloud_server" "serverctl_master" { inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"] connection { - host = self.ipv4_address - type = "ssh" - user = "root" + host = self.ipv4_address + type = "ssh" + user = "root" private_key = file(var.pvt_key) } } @@ -33,7 +33,7 @@ resource "null_resource" "configure_serverctl_master" { count = var.serverctl_master_count provisioner "local-exec" { - command = "ANSIBLE_HOST_KEY_CHECKING=False /usr/bin/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" + command = "ANSIBLE_HOST_KEY_CHECKING=False /usr/bin/ansible-playbook -u root -i '${element(hcloud_server.serverctl_master.*.ipv4_address, 0)},' --private-key ${var.pvt_key} -e 'pub_key=${var.pub_key}' server-install.yml" } triggers = { @@ -43,7 +43,16 @@ resource "null_resource" "configure_serverctl_master" { output "master_ipv4_addresses" { value = { - for serverctl in hcloud_server.serverctl_master: + for serverctl in hcloud_server.serverctl_master : serverctl.name => serverctl.ipv4_address } } + +resource "local_file" "hosts_cfg" { + content = templatefile("${path.module}/templates/hosts.tpl", + { + serverctl_masters = hcloud_server.serverctl_master.*.ipv4_address + } + ) + filename = "ansible/inventory/hosts.cfg" +} diff --git a/infrastructure/create-resources/provider.tf b/infrastructure/create-resources/provider.tf index e06523a..5c307d3 100644 --- a/infrastructure/create-resources/provider.tf +++ b/infrastructure/create-resources/provider.tf @@ -1,23 +1,23 @@ terraform { required_providers { hcloud = { - source = "hetznercloud/hcloud" + source = "hetznercloud/hcloud" version = "1.32.2" } } backend "s3" { bucket = "serverctl-terraform" - key = "terraform.tfstate" + key = "terraform.tfstate" endpoint = "https://api.minio.front.kjuulh.io" region = "main" skip_credentials_validation = true - skip_metadata_api_check = true - skip_region_validation = true - force_path_style = true + skip_metadata_api_check = true + skip_region_validation = true + force_path_style = true } } @@ -33,4 +33,3 @@ provider "hcloud" { variable "hcloud_serverctl_ssh_key_id" {} variable "pvt_key" {} variable "pub_key" {} - diff --git a/infrastructure/create-resources/templates/hosts.tpl b/infrastructure/create-resources/templates/hosts.tpl new file mode 100644 index 0000000..5320ea4 --- /dev/null +++ b/infrastructure/create-resources/templates/hosts.tpl @@ -0,0 +1,4 @@ +[serverctl_master_hosts] +%{ for ip in serverctl_masters ~} +${ip} +%{ endfor ~} \ No newline at end of file