variable “test” {
type = string
}
default = ~~로 선언하지 않으면, 적용 시 사용자에게 물어본다. (Enter a value)

variable "test" {
type = list(string)
default = ["value1", "value2", "value3"]
}
default = {
name = “name1”
id = “xxxx”
}

variable "instance" {
default = {
image_name = "CentOS7"
image_id = "973a59a8-ff61-4404-9e08-d281cdf5f257"
count = 3
}
}
variable "inside" {
default = {
name = "private1"
id = "ab0bc22b-f90b-49a2-8182-9a458101620f"
}
}
variable "outside" {
default = {
name = "extnet"
id = "98facebd-53fc-4a1c-80be-013026471da4"
}
}
variable "flavor" {
default = {
id = "6"
name = "m1.sm"
}
}
variable "keypair" {
default = {
name = "terraformkey"
}
}
resource "openstack_compute_instance_v2" "instance" {
name = "instance-${count.index +1}"
image_id = var.instance["image_id"]
flavor_name = var.flavor["name"]
key_pair = var.keypair["name"]
security_groups = ["icmp", "webssh"]
count = var.instance["count"]
network {
name = var.inside["name"]
}
}
terraform apply

main.tf 파일에 resource를 작성/프로비전 하기
Provisioner: file | Terraform | HashiCorp Developer
resource "openstack_compute_instance_v2" "instance" {
name = "instance-${count.index +1}"
image_name = var.instance["image_name"]
flavor_name = var.flavor["name"]
key_pair = var.keypair["name"]
security_groups = ["webssh", "icmp"]
count = var.instance["count"]
network {
name = var.inside["name"]
}
}
resource "openstack_compute_instance_v2" "control" {
name = "control"
image_name = var.instance["image_name"]
flavor_name = var.flavor["name"]
key_pair = var.keypair["name"]
security_groups = ["webssh", "icmp"]
network {
name = var.inside["name"]
}
provisioner "file" {
source = "/root/lab3/terraformkey.pem"
destination = "/home/centos/.ssh/id_rsa"
connection {
type = "ssh"
user = "centos"
private_key = file("/root/lab3/terraformkey.pem")
host = "${openstack_networking_floatingip_v2.fip2.address}"
}
}
}
resource "openstack_networking_floatingip_v2" "fip1" {
pool = var.outside["name"]
count = var.instance["count"]
}
resource "openstack_compute_floatingip_associate_v2" "fip1" {
count = var.instance["count"]
floating_ip = "${openstack_networking_floatingip_v2.fip1[count.index].address}"
instance_id = "${openstack_compute_instance_v2.instance[count.index].id}"
}
resource "openstack_networking_floatingip_v2" "fip2" {
pool = var.outside["name"]
}
resource "openstack_compute_floatingip_associate_v2" "fip2" {
floating_ip = "${openstack_networking_floatingip_v2.fip2.address}"
instance_id = "${openstack_compute_instance_v2.control.id}"
fixed_ip = "${openstack_compute_instance_v2.control.network.0.fixed_ip_v4}"
}
output "private_address" {
value = "${openstack_compute_instance_v2.instance[*].network.0.fixed_ip_v4}"
}
output "public_address" {
value = "${openstack_compute_floatingip_associate_v2.fip1[*].floating_ip}"
}
output "public_address_control" {
value = "${openstack_compute_floatingip_associate_v2.fip2.floating_ip}"
}
설치가 진행될 때 control은 즉시 fip가 붙지 않는다.
이때 오픈스택 인스턴스 리스트에서 오른쪽 에 있는 메뉴 중 “유동 IP 연결”클릭하면 발행된 fip 확인 가능하고 이를 연결하고 기다린다.


terraformkey.ppk를 SSH Auth에 넣어 실행후 id_rsa를 600으로 바꾸어 준 후, instance에 접속해본다
[centos@control ~]$ chmod 600 ~/.ssh/id_rsa
[centos@control ~]$ ssh 211.183.3.221

remote-exec 또는 user-data를 사용해 control에 ansible을 설치할 수 있다.
sudo yum install -y epel-release
sudo yum install -y ansible
echo "192.168.101.[201:239]" | sudo tee /etc/ansible/hosts
resource "openstack_compute_instance_v2" "instance" {
name = "instance-${count.index +1}"
image_name = var.instance["image_name"]
flavor_name = var.flavor["name"]
key_pair = var.keypair["name"]
security_groups = ["webssh", "icmp"]
count = var.instance["count"]
network {
name = var.inside["name"]
}
}
resource "openstack_compute_instance_v2" "control" {
name = "control"
image_name = var.instance["image_name"]
flavor_name = var.flavor["name"]
key_pair = var.keypair["name"]
security_groups = ["webssh", "icmp"]
user_data = file("ansibleinstall.sh")
network {
name = var.inside["name"]
}
provisioner "file" {
source = "/root/lab3/terraformkey.pem"
destination = "/home/centos/.ssh/id_rsa"
connection {
type = "ssh"
user = "centos"
private_key = file("/root/lab3/terraformkey.pem")
host = "${openstack_networking_floatingip_v2.fip2.address}"
}
}
}
sudo ps -ef | grep ansible
ansible -v
chmod 600 /home/centos/.ssh/id_rsa
sudo ansible all -m shell -a "hostname"