Add ansible to pipeline
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Kasper Juul Hermansen 2022-02-19 18:18:19 +01:00
parent 81abb30846
commit 170c5e0730
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
6 changed files with 32 additions and 35 deletions

View File

@ -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

View File

@ -0,0 +1,2 @@
[serverctl_master_hosts]
65.21.153.18

View File

@ -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

View File

@ -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"
}

View File

@ -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" {}

View File

@ -0,0 +1,4 @@
[serverctl_master_hosts]
%{ for ip in serverctl_masters ~}
${ip}
%{ endfor ~}