Cloud Solution Architect/Terraform, Ansible
GCP - Terraform, Ansible
YunHyeong
2023. 5. 19. 17:43
1. Service account
- 키를 만들면 json 형식의 파일이 하나 다운 받아진다.
- 이 파일을 credentials.json으로 변경하여 저장해 놓는다.
# vi provider.tf
provider "google" {
credentials = file("credentials.json")
project = "terraform-02"
region = "asia-northeast3"
zone = "asia-northeast3-a"
}
# vi main.tf
resource "google_compute_subnetwork" "network-with-private-ip-ranges" {
name = "test-subnet"
ip_cidr_range = "192.168.0.0/16"
region = "asia-northeast3"
network = google_compute_network.custom-test.id
}
resource "google_compute_network" "custom-test" {
name = "test-vpc"
auto_create_subnetworks = false
}
resource "google_compute_instance" "default" {
name = "foodwagon"
machine_type = "e2-micro"
zone = "asia-northeast3-a"
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-1804-lts"
}
}
network_interface {
network = google_compute_network.custom-test.id
subnetwork = google_compute_subnetwork.network-with-private-ip-ranges.id
access_config {
// Include this section to give the VM an external ip address
}
}
metadata_startup_script = file("/root/gcp_cli/httpd-gcp.txt")
// Apply the firewall rule to allow external IPs to access this instance
tags = ["http-server", "https-server", "ssh-server"]
}
resource "google_compute_firewall" "http-server" {
name = "test-vpc-http-terraform"
network = google_compute_network.custom-test.id
allow {
protocol = "tcp"
ports = ["80"]
}
// Allow traffic from everywhere to instances with an http-server tag
source_ranges = ["0.0.0.0/0"]
target_tags = ["http-server"]
}
resource "google_compute_firewall" "https-server" {
name = "test-vpc-https-terraform"
network = google_compute_network.custom-test.id
allow {
protocol = "tcp"
ports = ["443"]
}
// Allow traffic from everywhere to instances with an http-server tag
source_ranges = ["0.0.0.0/0"]
target_tags = ["https-server"]
}
# vi output.tf
output "ip" {
value = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}"
}
- vpc, instance terraform으로 생성
- 결과
2. 앤서블
2.1 앤서블 서버 설치
yum install epel-release -y
yum --enablerepo=epel -y install ansible
ansible --version
# vi /etc/ansible/hosts
[centos]
192.168.1.215
[ubuntu]
192.168.1.194
- centos와 ubuntu 주소를 hosts파일 맨 마지막에 넣어준다.
- ansible all -m ping 명령어 실행
- 하지만 ssh 패스워드 입력이 없었기 때문에 막힌다.
- 일반사용자로 우분투에 로그인은 가능하다
- 하지만 centos와 비밀번호가 다르기 때문에 둘을 따로 관리해야한다; 불편하다
- ubuntu server에서 root 사용자를 사용할 수 있도록 만든다.
- 하지만 iac server에서 root사용자로 접근시 root사용자를 사용할 수 없었다
- ubuntu 서버에서 cd /etc/ssh로 이동 후
- vi /etc/ssh/sshd_config 파일로 접근하여 PermitRootLogin을 yes로 변경한다.
- iac 서버에서 ansible all -m ping -k를 다시 입력하여 두 서버 모두 root로 접근