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 - name: terraform plan
image: alpine image: alpine
environment: environment:
HCLOUD_TOKEN: HCLOUD_TOKEN:
from_secret: serverctl_hcloud_token from_secret: serverctl_hcloud_token
ACCESS_KEY: ACCESS_KEY:
from_secret: serverctl_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 init -backend-config="access_key=$ACCESS_KEY" -backend-config="secret_key=$SECRET_KEY"
- terraform validate - 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" - 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 - become: yes
hosts: all hosts: all
name: apache-install name: server-install
tasks: tasks:
- name: Add the user 'kjuulh' and add it to 'sudo' - name: Add the user 'kjuulh' and add it to 'sudo'
user: user:
@ -14,22 +14,3 @@
- name: Wait for apt to unlock - name: Wait for apt to unlock
become: yes become: yes
shell: while sudo fuser /var/lib/dpkg/lock >/dev/null >2&1; do sleep 5; done; 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" { resource "hcloud_server" "serverctl_master" {
count = var.serverctl_master_count count = var.serverctl_master_count
name = "serverctl-master-${count.index}" name = "serverctl-master-${count.index}"
image = "debian-11" image = "debian-11"
server_type = "cx11" server_type = "cx11"
ssh_keys = [ ssh_keys = [
var.hcloud_serverctl_ssh_key_id 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!"] inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"]
connection { connection {
host = self.ipv4_address host = self.ipv4_address
type = "ssh" type = "ssh"
user = "root" user = "root"
private_key = file(var.pvt_key) private_key = file(var.pvt_key)
} }
} }
@ -33,7 +33,7 @@ resource "null_resource" "configure_serverctl_master" {
count = var.serverctl_master_count count = var.serverctl_master_count
provisioner "local-exec" { 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 = { triggers = {
@ -43,7 +43,16 @@ resource "null_resource" "configure_serverctl_master" {
output "master_ipv4_addresses" { output "master_ipv4_addresses" {
value = { value = {
for serverctl in hcloud_server.serverctl_master: for serverctl in hcloud_server.serverctl_master :
serverctl.name => serverctl.ipv4_address 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 { terraform {
required_providers { required_providers {
hcloud = { hcloud = {
source = "hetznercloud/hcloud" source = "hetznercloud/hcloud"
version = "1.32.2" version = "1.32.2"
} }
} }
backend "s3" { backend "s3" {
bucket = "serverctl-terraform" bucket = "serverctl-terraform"
key = "terraform.tfstate" key = "terraform.tfstate"
endpoint = "https://api.minio.front.kjuulh.io" endpoint = "https://api.minio.front.kjuulh.io"
region = "main" region = "main"
skip_credentials_validation = true skip_credentials_validation = true
skip_metadata_api_check = true skip_metadata_api_check = true
skip_region_validation = true skip_region_validation = true
force_path_style = true force_path_style = true
} }
} }
@ -33,4 +33,3 @@ provider "hcloud" {
variable "hcloud_serverctl_ssh_key_id" {} variable "hcloud_serverctl_ssh_key_id" {}
variable "pvt_key" {} variable "pvt_key" {}
variable "pub_key" {} variable "pub_key" {}

View File

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