Last active
January 21, 2020 13:20
-
-
Save Xophmeister/11affbaf97c81185f74c63cd10713094 to your computer and use it in GitHub Desktop.
Create a group of homogeneous machines, with SSH access, instance metadata and user data, in OpenStack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
declare INDEX="$(cloud-init query ds.meta_data.meta.index)" | |
declare TOTAL="$(cloud-init query ds.meta_data.meta.total)" | |
echo "Hello, World! This is instance $(( INDEX + 1 )) of ${TOTAL}." \ | |
| tee /root/hello.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Providers ########################################################### | |
provider "local" {} | |
provider "template" {} | |
provider "openstack" { | |
# The setup for this is taken from the environment | |
# See https://www.terraform.io/docs/providers/openstack | |
} | |
## Variables ########################################################### | |
variable network { | |
description = "Basic networking configuration" | |
default = { | |
subnet = "10.1.0.0/24" | |
} | |
} | |
variable instance { | |
description = "Basic instance configuration" | |
default = { | |
flavour = "m2.medium" | |
image = "bionic-server" | |
count = 1 | |
} | |
} | |
variable "public_key_file" { | |
description = "SSH public key file" | |
default = "~/.ssh/id_rsa.pub" | |
} | |
variable "user_script" { | |
description = "User data script file" | |
default = "hello-world.sh" | |
} | |
## User Data ########################################################### | |
data "local_file" "user_data" { | |
filename = "${var.user_script}" | |
} | |
data "template_cloudinit_config" "user_data" { | |
gzip = true | |
base64_encode = true | |
part { | |
filename = "${data.local_file.user_data.filename}" | |
content_type = "text/x-shellscript" | |
content = "${chomp(data.local_file.user_data.content)}" | |
} | |
} | |
## Security ############################################################ | |
data "local_file" "public_key" { | |
filename = "${pathexpand(var.public_key_file)}" | |
} | |
resource "openstack_compute_keypair_v2" "public_key" { | |
name = "hello_world" | |
public_key = "${chomp(data.local_file.public_key.content)}" | |
} | |
resource "openstack_networking_secgroup_v2" "secgroup" { | |
name = "hello_world" | |
} | |
resource "openstack_networking_secgroup_rule_v2" "ssh_in" { | |
description = "SSH in" | |
direction = "ingress" | |
ethertype = "IPv4" | |
protocol = "tcp" | |
port_range_min = 22 | |
port_range_max = 22 | |
remote_ip_prefix = "0.0.0.0/0" | |
security_group_id = "${openstack_networking_secgroup_v2.secgroup.id}" | |
} | |
## Networking ########################################################## | |
data "openstack_networking_network_v2" "external" { | |
external = true | |
} | |
resource "openstack_networking_router_v2" "router" { | |
name = "hello_world" | |
external_network_id = "${data.openstack_networking_network_v2.external.id}" | |
} | |
resource "openstack_networking_network_v2" "network" { | |
name = "hello_world" | |
} | |
resource "openstack_networking_subnet_v2" "subnet" { | |
name = "hello_world" | |
network_id = "${openstack_networking_network_v2.network.id}" | |
cidr = "${var.network["subnet"]}" | |
ip_version = 4 | |
} | |
resource "openstack_networking_router_interface_v2" "interface" { | |
router_id = "${openstack_networking_router_v2.router.id}" | |
subnet_id = "${openstack_networking_subnet_v2.subnet.id}" | |
} | |
resource "openstack_networking_floatingip_v2" "ip" { | |
pool = "${data.openstack_networking_network_v2.external.name}" | |
description = "Hello World IP ${count.index}" | |
count = "${var.instance["count"]}" | |
} | |
## Compute ############################################################# | |
data "openstack_compute_flavor_v2" "standard" { | |
name = "${var.instance["flavour"]}" | |
} | |
resource "openstack_compute_instance_v2" "instance" { | |
name = "hello-world-${count.index}" | |
flavor_id = "${data.openstack_compute_flavor_v2.standard.flavor_id}" | |
image_name = "${var.instance["image"]}" | |
key_pair = "${openstack_compute_keypair_v2.public_key.name}" | |
security_groups = ["${openstack_networking_secgroup_v2.secgroup.name}"] | |
network { name = "${openstack_networking_network_v2.network.name}" } | |
user_data = "${data.template_cloudinit_config.user_data.rendered}" | |
metadata = { | |
index = "${count.index}" | |
total = "${var.instance["count"]}" | |
} | |
count = "${var.instance["count"]}" | |
} | |
resource "openstack_compute_floatingip_associate_v2" "public_ip" { | |
floating_ip = "${openstack_networking_floatingip_v2.ip.*.address[count.index]}" | |
instance_id = "${openstack_compute_instance_v2.instance.*.id[count.index]}" | |
count = "${var.instance["count"]}" | |
} | |
## Output ############################################################## | |
output "resources" { | |
value = { | |
instances = "${var.instance["count"]}" | |
cores = "${data.openstack_compute_flavor_v2.standard.vcpus}" | |
memory = "${data.openstack_compute_flavor_v2.standard.ram}" | |
ips = "${openstack_networking_floatingip_v2.ip.*.address}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment