Skip to content

Commit 9c7f9be

Browse files
committed
try setup remote ansible
1 parent f66ac09 commit 9c7f9be

8 files changed

+341
-313
lines changed

.github/workflows/apply.yml

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Manual Apply # To prevent using a stale plan file, please run the Integrate workflow again.
1+
name: Manual Apply
22

33
on:
44
workflow_dispatch:
@@ -20,6 +20,11 @@ jobs:
2020
with:
2121
terraform_version: 1.6
2222

23+
- name: Setup SSH Private Key
24+
run: |
25+
echo "${{ secrets.AWS_PRIVATE_KEY }}" > b2111933-pair.pem
26+
chmod 600 b2111933-pair.pem
27+
2328
- name: Download Planfile Artifact
2429
uses: dawidd6/action-download-artifact@v7
2530
with:
@@ -31,19 +36,3 @@ jobs:
3136

3237
- name: Terraform Apply
3338
run: terraform apply -input=false planfile
34-
35-
config:
36-
runs-on: ubuntu-latest
37-
needs: apply # Run after terraform apply
38-
steps:
39-
- name: Checkout Code
40-
uses: actions/checkout@v3
41-
42-
- name: Set up Ansible
43-
run: |
44-
sudo apt update
45-
sudo apt install -y ansible
46-
47-
- name: Run Ansible Playbook
48-
run: |
49-
ansible-playbook -i ansible/inventory/aws_ec2.yaml ansible/playbooks/nginx.yml

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ override.tf.json
3737
# Ignore CLI configuration files
3838
.terraformrc
3939
terraform.rc
40+
41+
# Keys
42+
b2111933-pair.pem

.terraform.lock.hcl

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ec2/main.tf

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ resource "aws_instance" "amazon" {
99
subnet_id = var.public_subnet[0]
1010
vpc_security_group_ids = [var.security_group["sg_linux"]]
1111

12+
provisioner "remote-exec" {
13+
inline = [
14+
"sudo dnf update -y",
15+
"sudo dnf install -y amazon-linux-extras",
16+
"sudo amazon-linux-extras enable ansible2",
17+
"sudo dnf install -y ansible"
18+
]
19+
connection {
20+
type = "ssh"
21+
user = "ec2-user"
22+
private_key = file(var.private_key_path)
23+
host = self.public_ip
24+
}
25+
}
26+
1227
tags = {
1328
Name = "B2111933 Amazon Linux"
1429
}
@@ -25,6 +40,21 @@ resource "aws_instance" "ubuntu" {
2540
subnet_id = var.public_subnet[1]
2641
vpc_security_group_ids = [var.security_group["sg_linux"]]
2742

43+
provisioner "remote-exec" {
44+
inline = [
45+
"sudo apt update -y",
46+
"sudo apt install -y software-properties-common",
47+
"sudo apt-add-repository --yes --update ppa:ansible/ansible",
48+
"sudo apt install -y ansible"
49+
]
50+
connection {
51+
type = "ssh"
52+
user = "ubuntu"
53+
private_key = file(var.private_key_path)
54+
host = self.public_ip
55+
}
56+
}
57+
2858
tags = {
2959
Name = "B2111933 Ubuntu"
3060
}

ec2/variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ variable "instance_type_free" {
3434
type = string
3535
default = "t2.micro"
3636
}
37+
38+
variable "private_key_path" {
39+
type = string
40+
default = "b2111933-pair.pem"
41+
}

tests/1-backend.tftest.hcl

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
//just for practice
1+
# //just for practice
22

3-
variables {
4-
bucket = "b2111933-tfbucket"
5-
dynamodb_table = "b2111933-tftable"
6-
dynamodb_key = "b2111933-tfstate"
7-
//more
8-
}
3+
# variables {
4+
# bucket = "b2111933-tfbucket"
5+
# dynamodb_table = "b2111933-tftable"
6+
# dynamodb_key = "b2111933-tfstate"
7+
# //more
8+
# }
99

10-
run "bucket_tests" {
11-
command = plan
12-
assert {
13-
condition = var.bucket == "b2111933-tfbucket"
14-
error_message = "Not our bucket!"
15-
}
16-
}
10+
# run "bucket_tests" {
11+
# command = plan
12+
# assert {
13+
# condition = var.bucket == "b2111933-tfbucket"
14+
# error_message = "Not our bucket!"
15+
# }
16+
# }
1717

18-
run "dynamodb_table_tests" {
19-
command = plan
20-
assert {
21-
condition = var.dynamodb_table == "b2111933-tftable"
22-
error_message = "Not our table!"
23-
}
24-
}
18+
# run "dynamodb_table_tests" {
19+
# command = plan
20+
# assert {
21+
# condition = var.dynamodb_table == "b2111933-tftable"
22+
# error_message = "Not our table!"
23+
# }
24+
# }
2525

26-
run "dynamodb_key_tests" {
27-
command = plan
28-
assert {
29-
condition = var.dynamodb_key == "b2111933-tfstate"
30-
error_message = "Not a valid key!"
31-
}
32-
}
26+
# run "dynamodb_key_tests" {
27+
# command = plan
28+
# assert {
29+
# condition = var.dynamodb_key == "b2111933-tfstate"
30+
# error_message = "Not a valid key!"
31+
# }
32+
# }
3333

tests/2-unit.tftest.hcl

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
# Unit tests for the VPC and EC2 modules
1+
# # Unit tests for the VPC and EC2 modules
22

3-
run "vpc_tests" {
4-
command = plan
3+
# run "vpc_tests" {
4+
# command = plan
55

6-
assert {
7-
condition = module.vpc.vpc_dns_hostnames_enabled == true
8-
error_message = "Must enable DNS hostname resolution!"
9-
}
6+
# assert {
7+
# condition = module.vpc.vpc_dns_hostnames_enabled == true
8+
# error_message = "Must enable DNS hostname resolution!"
9+
# }
1010

11-
assert {
12-
condition = module.vpc.vpc_dns_support_enabled == true
13-
error_message = "Must enable DNS resolution!"
14-
}
15-
}
11+
# assert {
12+
# condition = module.vpc.vpc_dns_support_enabled == true
13+
# error_message = "Must enable DNS resolution!"
14+
# }
15+
# }
1616

17-
run "ec2_tests" {
18-
command = plan
17+
# run "ec2_tests" {
18+
# command = plan
1919

20-
assert {
21-
condition = module.ec2.amazon_instance_type[0] == "t2.micro"
22-
error_message = "Amazon Linux instance must be t2.micro for free tier!"
23-
}
20+
# assert {
21+
# condition = module.ec2.amazon_instance_type[0] == "t2.micro"
22+
# error_message = "Amazon Linux instance must be t2.micro for free tier!"
23+
# }
2424

25-
assert {
26-
condition = module.ec2.ubuntu_instance_type[0] == "t2.micro"
27-
error_message = "Ubuntu instance must be t2.micro for free tier!"
28-
}
25+
# assert {
26+
# condition = module.ec2.ubuntu_instance_type[0] == "t2.micro"
27+
# error_message = "Ubuntu instance must be t2.micro for free tier!"
28+
# }
2929

30-
assert {
31-
condition = module.ec2.windows_instance_type[0] == "t2.micro"
32-
error_message = "Windows instance must be t2.micro for free tier!"
33-
}
34-
}
30+
# assert {
31+
# condition = module.ec2.windows_instance_type[0] == "t2.micro"
32+
# error_message = "Windows instance must be t2.micro for free tier!"
33+
# }
34+
# }

0 commit comments

Comments
 (0)