Skip to content

Commit fa031dd

Browse files
Merge pull request #1 from infraspecdev/add-ansible-playbook
Add ansible playbook
2 parents 03ae095 + df9c34b commit fa031dd

File tree

9 files changed

+147
-7
lines changed

9 files changed

+147
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nginx['redirect_http_to_https'] = false
2+
nginx['listen_port'] = 80
3+
nginx['listen_https'] = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postgresql['enable'] = false
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
external_url '${gitlab_url}'
2+
3+
gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0','127.0.0.0/8', '::1/128']
4+
5+
gitlab_rails['db_adapter'] = "postgresql"
6+
gitlab_rails['db_encoding'] = "unicode"
7+
gitlab_rails['db_database'] = "${gitlab_db_name}"
8+
gitlab_rails['db_username'] = "${gitlab_db_username}"
9+
gitlab_rails['db_password'] = "${gitlab_db_password}"
10+
gitlab_rails['db_host'] = "${gitlab_db_host}"
11+
12+
gitlab_rails['redis_host'] = "${gitlab_redis_host}"
13+
gitlab_rails['redis_port'] = 6379
14+
15+
letsencrypt['enable'] = false
16+
17+
gitlab_rails['backup_upload_connection'] = {
18+
'provider' => 'AWS',
19+
'region' => '${aws_region}',
20+
'use_iam_profile' => true
21+
}
22+
gitlab_rails['backup_upload_remote_directory'] = '${gitlab_backup_s3_bucket_name}'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redis['enable'] = false

main.tf

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
locals {
2-
managed_by = "Terraform"
2+
managed_by = "Terraform"
3+
gitlab_config_file_name = "gitlab.rb"
4+
rendered_gitlab_config_file_name = "gitlab_rendered.rb"
5+
gitlab_additional_config_file_name = "gitlab_additional.rb"
6+
gitlab_config_tmp_path = "/tmp/gitlab/gitlab_config"
7+
gitlab_config_template_file_path = "${path.module}/gitlab_config_templates"
8+
gitlab_config_file_path = "${path.cwd}/gitlab_config"
9+
gitlab_config_playbook_file = "${path.module}/playbooks/gitlab_setup.yaml"
10+
gitlab_complete_url = join("", tolist(["https://", values(module.records.route53_record_name)[0]]))
311
}
412

513
resource "aws_instance" "gitlab" {
@@ -16,11 +24,13 @@ resource "aws_instance" "gitlab" {
1624
volume_size = var.volume_size
1725
delete_on_termination = false
1826
}
27+
1928
tags = {
2029
Name = "${var.environment_prefix}-gitlab"
2130
Environment = var.environment_prefix
2231
ManagedBy = local.managed_by
2332
}
33+
2434
}
2535

2636
resource "aws_key_pair" "gitlab_ssh" {
@@ -224,12 +234,6 @@ module "elb" {
224234
unhealthy_threshold = var.healthcheck_unhealthy_threshold
225235
timeout = var.healthcheck_timeout
226236
}
227-
#
228-
# access_logs = {
229-
# bucket = "my-access-logs-bucket"
230-
# }
231-
232-
// ELB attachments
233237
number_of_instances = length(aws_instance.gitlab)
234238
instances = aws_instance.gitlab[*].id
235239

@@ -449,3 +453,46 @@ resource "aws_iam_instance_profile" "gitlab" {
449453
name = "gitlab"
450454
role = aws_iam_role.gitlab_backup.name
451455
}
456+
457+
data "template_file" "gitlab_config_template" {
458+
template = join("\n", [
459+
for fn in fileset(".", "${local.gitlab_config_template_file_path}/**") : file(fn)
460+
])
461+
vars = {
462+
gitlab_url = local.gitlab_complete_url,
463+
gitlab_db_name = module.gitlab_pg.db_instance_name,
464+
gitlab_db_username = module.gitlab_pg.db_instance_username,
465+
gitlab_db_password = module.gitlab_pg.db_instance_password,
466+
gitlab_db_host = module.gitlab_pg.db_instance_address,
467+
gitlab_redis_host = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address,
468+
aws_region = aws_s3_bucket.gitlab_backup[0].region
469+
gitlab_backup_s3_bucket_name = aws_s3_bucket.gitlab_backup[0].bucket
470+
}
471+
}
472+
473+
resource "local_sensitive_file" "rendered_gitlab_config_file" {
474+
filename = "${local.gitlab_config_tmp_path}/${local.rendered_gitlab_config_file_name}"
475+
content = data.template_file.gitlab_config_template.rendered
476+
}
477+
478+
data "local_sensitive_file" "gitlab_additional_config" {
479+
count = fileexists("${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}") ? 1 : 0
480+
filename = "${local.gitlab_config_file_path}/${local.gitlab_additional_config_file_name}"
481+
}
482+
483+
resource "local_sensitive_file" "gitlab_config_file" {
484+
filename = "${local.gitlab_config_tmp_path}/${local.gitlab_config_file_name}"
485+
content = join("\n", tolist([
486+
data.template_file.gitlab_config_template.rendered,
487+
data.local_sensitive_file.gitlab_additional_config != [] ? data.local_sensitive_file.gitlab_additional_config[0].content : ""
488+
]))
489+
}
490+
491+
resource "null_resource" "gitlab_reconfigure" {
492+
triggers = {
493+
timestamp = timestamp()
494+
}
495+
provisioner "local-exec" {
496+
command = "ansible-playbook -u ubuntu -i '${aws_instance.gitlab[0].private_ip},' --private-key ${var.private_key} -e 'instance_ip_address=${aws_instance.gitlab[0].private_ip} workdir=${local.gitlab_config_tmp_path} config_file=${local_sensitive_file.gitlab_config_file.filename}' ${local.gitlab_config_playbook_file}"
497+
}
498+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ output "gitlab_redis_address" {
3737
value = aws_elasticache_cluster.gitlab_redis.cache_nodes[0].address
3838
description = "Gitlab Redis cluster address"
3939
}
40+
41+
output "gitlab_complete_url" {
42+
value = local.gitlab_complete_url
43+
}

playbooks/gitlab_setup.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
- name: Configure Gitlab
3+
hosts: "{{ instance_ip_address }}"
4+
gather_facts: no
5+
vars:
6+
ansible_host_key_checking: false
7+
update_gitlab_config: false
8+
tasks:
9+
- local_action: wait_for port=22 host="{{ instance_ip_address }}" delay=5 timeout=300
10+
11+
- name: stat for /etc/gitlab/gitlab.rb
12+
become: true
13+
stat:
14+
path: "/etc/gitlab/gitlab.rb"
15+
register: original_config_file
16+
- name: Checksum for original gitlab.rb"
17+
set_fact:
18+
original_config_file_checksum: "{{ original_config_file.stat.checksum }}"
19+
- name: print original original checksum
20+
debug:
21+
msg: "{{ original_config_file_checksum }}"
22+
23+
- name: stat for "{{ config_file }}"
24+
local_action: stat path={{ config_file }}
25+
register: new_config_file
26+
- name: Checksum for new gitlab.rb"
27+
set_fact:
28+
new_config_file_checksum: "{{ new_config_file.stat.checksum }}"
29+
- name: print new file checksum
30+
debug:
31+
msg: "{{ new_config_file_checksum }}"
32+
33+
- name: Update gitlab.rb
34+
set_fact:
35+
update_gitlab_config: true
36+
when: original_config_file_checksum != new_config_file_checksum
37+
38+
- name: copy gitlab.rb to /etc/gitlab/
39+
become: true
40+
when: update_gitlab_config
41+
copy:
42+
src: "{{ config_file }}"
43+
dest: "/etc/gitlab/gitlab.rb"
44+
owner: "root"
45+
group: "root"
46+
mode: 0600
47+
- name: reconfigure Gitlab
48+
become: true
49+
when: update_gitlab_config
50+
command: gitlab-ctl reconfigure
51+
52+
- name: cleanup temp files
53+
local_action: command rm -rf {{ workdir }}

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,8 @@ variable "gitlab_backup_bucket_name" {
268268
default = null
269269
description = "Name of S3 bucket to be used for Gitlab backup"
270270
}
271+
272+
variable "private_key" {
273+
type = string
274+
description = "Private key to execute ansible playbook on Gitlab instance."
275+
}

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 4.40"
88
}
9+
null = {
10+
source = "hashicorp/null"
11+
version = ">= 3.2.1"
12+
}
913
}
1014
}

0 commit comments

Comments
 (0)