Skip to content
Snippets Groups Projects
Commit 6a0a1e36 authored by Ryan Randles Jones's avatar Ryan Randles Jones
Browse files

Merge branch 'make-modular' into 'master'

Make modular

See merge request rrand11/terraform-openstack!27
parents 1cd3ecf6 1251f46e
No related branches found
No related tags found
No related merge requests found
...@@ -73,6 +73,16 @@ unzip terraform_0.12.3_linux_amd64.zip -d ~/bin ...@@ -73,6 +73,16 @@ unzip terraform_0.12.3_linux_amd64.zip -d ~/bin
- `$terraform apply "$HOME/terraform-first-instance/terraform-plan.tf"` - `$terraform apply "$HOME/terraform-first-instance/terraform-plan.tf"`
### There's a target parameter that will let you specify just one module as defined in the root main file. Run it via plan like so:
- `$terraform plan -out $HOME/terraform-first-instance/name-of-plan.tf -target=module.name-of-module-to-run`
**_(Note)_** You can also target multiple modules.
- `$terraform plan -out $HOME/terraform-first-instance/name-of-plan.tf -target=module.name-of-module -target=module.name-of-other-module`
### Destroy Terraform Instance: ### Destroy Terraform Instance:
- `$terraform destroy` - `$terraform destroy`
data "openstack_networking_network_v2" "public-network" {
name = var.public-network-name
}
# is created as a datasource this module and called in root module
variable "public_network_name" {type = "string"}
variable "name" {default = "dmz"}
variable "admin_state_up" {}
variable "enable_dhcp" {}
data "openstack_networking_network_v2" "public_network" {name = var.public_network_name}
# creates dmznet
resource "openstack_networking_network_v2" "external_network" {
name = "${var.name}net"
admin_state_up = var.admin_state_up
}
resource "openstack_networking_subnet_v2" "external_subnet" {
name = "${var.name}subnet"
network_id = openstack_networking_network_v2.external_network.id
cidr = "192.168.100.0/24"
ip_version = 4
dns_nameservers = ["8.8.8.8"]
enable_dhcp = var.enable_dhcp
}
# defines the router dmzrouter using floating ip defined in datasource above to create the external network id
resource "openstack_networking_router_v2" "router" {
name = "${var.name}router"
admin_state_up = var.admin_state_up
external_network_id = data.openstack_networking_network_v2.public_network.id
}
resource "openstack_networking_router_interface_v2" "router" {
router_id = openstack_networking_router_v2.router.id
subnet_id = openstack_networking_subnet_v2.external_subnet.id
}
output "external_network_id" {
value = data.openstack_networking_network_v2.public_network.id
}
output "external_subnet_id" {
value = openstack_networking_subnet_v2.external_subnet.id
}
output "router_id" {
value = openstack_networking_router_v2.router.id
}
\ No newline at end of file
output "ohpc_address" { variable "public_network_name" {}
value = openstack_networking_floatingip_v2.ohpc_ip.address
}
output "ood_address" {
value = openstack_networking_floatingip_v2.ood_ip.address # defines where floating ip will come from using variable public_network_name defined in root module
resource "openstack_networking_floatingip_v2" "ohpc_ip" {
pool = var.public_network_name
} }
output "external_network_id" { resource "openstack_networking_floatingip_v2" "ood_ip" {
value = openstack_networking_network_v2.external_net.id pool = var.public_network_name
} }
output "internal_network_id" { output "ohpc_address" {
value = openstack_networking_network_v2.internal_net.id value = openstack_networking_floatingip_v2.ohpc_ip.address
} }
output "ood_address" {
value = openstack_networking_floatingip_v2.ood_ip.address
}
variable "name" {default = "cluster"}
variable "admin_state_up" { }
variable "enable_dhcp" {}
# creates clusternet
resource "openstack_networking_network_v2" "internal_network" {
name = "${var.name}net"
admin_state_up = var.admin_state_up
}
# creates clustersubnet
# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack)
resource "openstack_networking_subnet_v2" "internal_subnet" {
name = "${var.name}subnet"
network_id = openstack_networking_network_v2.internal_network.id
cidr = "10.1.1.0/24"
ip_version = 4
enable_dhcp = var.enable_dhcp
}
output "internal_network_id" {
value = openstack_networking_network_v2.internal_network.id
}
output "internal_subnet_id" {
value = openstack_networking_subnet_v2.internal_subnet.id
}
\ No newline at end of file
variable "keypair_name" {}
variable "ssh_public_key" {}
resource "openstack_compute_keypair_v2" "keypair" {
name = var.keypair_name
public_key = file(var.ssh_public_key)
}
output "keypair_name" {
value = var.keypair_name
}
output "public_key" {
value = file(var.ssh_public_key)
}
resource "openstack_compute_keypair_v2" "keypair" { # runs the external-network module
name = var.keypair-name module "dmz-network" {
public_key = file(var.ssh-public-key) source = "./external-network"
# Default name var is in the module main file
admin_state_up = var.admin_state_up
enable_dhcp = var.enable_dhcp
public_network_name = var.public_network_name
} }
# calls the outputs defined in the external-network module
# creates dmznet output "external_network_id" {
resource "openstack_networking_network_v2" "external_net" { value = "${module.dmz-network.external_network_id}"
name = var.external-net
admin_state_up = var.admin-state-up
} }
resource "openstack_networking_subnet_v2" "external_subnet" { output "router_id" {
name = var.external-subnet value = "${module.dmz-network.router_id}"
network_id = openstack_networking_network_v2.external_net.id
cidr = "192.168.100.0/24"
ip_version = 4
dns_nameservers = ["8.8.8.8"]
enable_dhcp = var.enable-dhcp
} }
# defines the router borderrouter using floating ip defined in datasources.tf to create the external network id # runs the internal-network module
resource "openstack_networking_router_v2" "router" { module "cluster-network" {
name = var.router source = "./internal-network"
admin_state_up = var.admin-state-up # Default name var is in the module main file
external_network_id = data.openstack_networking_network_v2.public-network.id admin_state_up = var.admin_state_up
enable_dhcp = var.enable_dhcp
} }
# calls the outputs defined in the internal-network module
resource "openstack_networking_router_interface_v2" "router" { output "internal_network_id" {
router_id = openstack_networking_router_v2.router.id value = "${module.cluster-network.internal_network_id}"
subnet_id = openstack_networking_subnet_v2.external_subnet.id
} }
# creates clusternet # runs the floating-ip module - uses public network name defined above
resource "openstack_networking_network_v2" "internal_net" { module "floating-ip-address" {
name = var.internal-net source = "./floating-ip"
admin_state_up = var.admin-state-up public_network_name = var.public_network_name
} }
# creates clustersubnet # calls the outputs defined in the floating-ip module
# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack) output "floating_ip_ohpc" {
resource "openstack_networking_subnet_v2" "internal_subnet" { value = "${module.floating-ip-address.ohpc_address}"
name = var.internal-subnet
network_id = openstack_networking_network_v2.internal_net.id
cidr = "10.1.1.0/24"
ip_version = 4
enable_dhcp = var.enable-dhcp
} }
# defines where floating ip will come from using variable from vars.tf output "floating_ip_ood" {
resource "openstack_networking_floatingip_v2" "ohpc_ip" { value = "${module.floating-ip-address.ood_address}"
pool = var.public-network-name
} }
resource "openstack_networking_floatingip_v2" "ood_ip" { # runs the key-pair module - imports local public key into openstack and give it the name defined above in the variables
pool = var.public-network-name module "import-keypair" {
source = "./key-pair"
keypair_name = var.keypair_name
ssh_public_key = var.ssh_public_key
} }
# creates details for the OHPC instance using variables defined in vars.tf # calls the outputs defined in the key-pair module
resource "openstack_compute_instance_v2" "ohpc" { output "keypair_name" {
depends_on = [openstack_networking_subnet_v2.external_subnet] value = "${module.import-keypair.keypair_name}"
name = var.ohpc-instance-name
image_name = var.image_ohpc
flavor_name = var.flavor
key_pair = openstack_compute_keypair_v2.keypair.name
security_groups = ["default"]
# defines the networks of the instance
network {
name = var.external-net
}
network {
name = var.internal-net
}
} }
# associates floating ip with the OHPC instance and run the ansible playbook # runs the ohpc-instance module - creates ohpc instance using variables defined above
resource "openstack_compute_floatingip_associate_v2" "ohpc" { # calls functions from dmz-network, import-keypair, and floating-ip-address modules to get values created there for use
floating_ip = openstack_networking_floatingip_v2.ohpc_ip.address module "create-ohpc-instance" {
instance_id = openstack_compute_instance_v2.ohpc.id external_subnet_id = "${module.dmz-network.external_subnet_id}"
source = "./ohpc-instance"
# defines ssh connection ohpc_instance_name = var.ohpc_instance_name
connection { image_ohpc = var.image_ohpc
host = format( flavor = var.flavor
var.host-prefix, key_pair = "${module.import-keypair.keypair_name}"
element( external_network = var.external_network
split(".", openstack_networking_floatingip_v2.ohpc_ip.address), internal_network = var.internal_network
3, floating_ip_ohpc = "${module.floating-ip-address.ohpc_address}"
), host_prefix = var.host_prefix
) ohpc_user = var.ohpc_user
user = var.ohpc-user ssh_private_key = var.ssh_private_key
private_key = file(var.ssh-private-key)
}
} }
# creates details for the OOD instance using variables defined in vars.tf # runs the ood-instance module - creates ood instance using variables defined above
resource "openstack_compute_instance_v2" "ood" { # calls functions from cluster-network, import-keypair, and floating-ip-address modules to get values created there for use
depends_on = [openstack_networking_subnet_v2.external_subnet] module "create-ood-instance" {
name = var.ood-instance-name internal_subnet_id = "${module.cluster-network.internal_subnet_id}"
image_name = var.image_ood source = "./ood-instance"
flavor_name = var.flavor ood_instance_name = var.ood_instance_name
key_pair = openstack_compute_keypair_v2.keypair.name image_ood = var.image_ood
security_groups = ["default"] flavor = var.flavor
key_pair = "${module.import-keypair.keypair_name}"
# defines the networks of the instance internal_network = var.internal_network
network { external_network = var.external_network
name = var.external-net floating_ip_ood = "${module.floating-ip-address.ood_address}"
} host_prefix = var.host_prefix
network { ood_user = var.ood_user
name = var.internal-net ssh_private_key = var.ssh_private_key
}
} }
# associates floating ip with the OOD instance and run the ansible playbook # runs the nodes module - creates nodes using variables defined above
resource "openstack_compute_floatingip_associate_v2" "ood" { # calls functions from cluster-network and import-keypair modules to get values created there for use
floating_ip = openstack_networking_floatingip_v2.ood_ip.address module "nodes" {
instance_id = openstack_compute_instance_v2.ood.id internal_subnet_id = "${module.cluster-network.internal_subnet_id}"
source = "./nodes"
# defines ssh connection image_compute = var.image_compute
connection { flavor = var.flavor
host = format( key_pair = "${module.import-keypair.keypair_name}"
var.host-prefix, compute_node_count = var.compute_node_count
element( internal_network = var.internal_network
split(".", openstack_networking_floatingip_v2.ood_ip.address),
3,
),
)
user = var.ood-user
private_key = file(var.ssh-private-key)
}
} }
# creates compute node # calls the outputs defined in the ohpc-instance module
resource "openstack_compute_instance_v2" "c0" { output "ohpc-ssh_host" {
depends_on = [openstack_networking_subnet_v2.internal_subnet] value = "${module.create-ohpc-instance.ssh_host}"
name = "c${count.index}" }
image_name = var.image_compute
flavor_name = var.flavor
key_pair = openstack_compute_keypair_v2.keypair.name
security_groups = ["default"]
count = var.compute_node_count
# defines the networks of the instance # calls the outputs defined in the ood-instance module
network { output "ood-ssh_host" {
name = var.internal-net value = "${module.create-ood-instance.ssh_host}"
}
} }
\ No newline at end of file
# is created in internal-network module and called in root module
variable "internal_subnet_id" {type = "string"}
variable "image_compute" {}
variable "flavor" {}
# is created in key-pair module and called in root module
variable "key_pair" {type = "string"}
variable "compute_node_count" { }
variable "internal_network" {}
# creates compute node
resource "openstack_compute_instance_v2" "c0" {
depends_on = [var.internal_subnet_id]
name = "c${count.index}"
image_name = var.image_compute
flavor_name = var.flavor
key_pair = var.key_pair
security_groups = ["default"]
count = var.compute_node_count
# defines the networks of the instance
network {
name = var.internal_network
}
}
\ No newline at end of file
# is created in external-network module and called in root module
variable "external_subnet_id" {type = "string"}
variable "ohpc_instance_name" {}
variable "image_ohpc" {}
variable "flavor" {}
# is created in key-pair module and called in root module
variable "key_pair" {type = "string"}
variable "internal_network" {}
variable "external_network" {}
# is created in floating-ip module and called in root module
variable "floating_ip_ohpc" {type = "string"}
variable "host_prefix" {}
variable "ohpc_user" {}
variable "ssh_private_key" {}
# creates details for the OHPC instance
resource "openstack_compute_instance_v2" "ohpc" {
depends_on = [var.external_subnet_id]
name = var.ohpc_instance_name
image_name = var.image_ohpc
flavor_name = var.flavor
key_pair = var.key_pair
security_groups = ["default"]
# defines the networks of the instance
network {
name = var.external_network
}
network {
name = var.internal_network
}
}
# associates floating ip with the OHPC instance
resource "openstack_compute_floatingip_associate_v2" "ohpc" {
floating_ip = var.floating_ip_ohpc
instance_id = openstack_compute_instance_v2.ohpc.id
# defines ssh connection
connection {
host = format(var.host_prefix,element(split(".", var.floating_ip_ohpc),3,),)
user = var.ohpc_user
private_key = file(var.ssh_private_key)
}
}
output "ssh_host" {
value = format(var.host_prefix,element(split(".", var.floating_ip_ohpc),3,),)
}
\ No newline at end of file
# is created in internal-network module and called in root module
variable "internal_subnet_id" {type = "string"}
variable "ood_instance_name" {}
variable "image_ood" {}
variable "flavor" {}
# is created in key-pair module and called in root module
variable "key_pair" {type = "string"}
variable "internal_network" {}
variable "external_network" {}
# is created in floating-ip module and called in root module
variable "floating_ip_ood" {type = "string"}
variable "host_prefix" {}
variable "ood_user" {}
variable "ssh_private_key" {}
# creates details for the OOD instance
resource "openstack_compute_instance_v2" "ood" {
depends_on = [var.internal_subnet_id]
name = var.ood_instance_name
image_name = var.image_ood
flavor_name = var.flavor
key_pair = var.key_pair
security_groups = ["default"]
# defines the networks of the instance
network {
name = var.external_network
}
network {
name = var.internal_network
}
}
# associates floating ip with the OOD instance
resource "openstack_compute_floatingip_associate_v2" "ood" {
floating_ip = var.floating_ip_ood
instance_id = openstack_compute_instance_v2.ood.id
# defines ssh connection
connection {
host = format(var.host-prefix,element(split(".", var.floating_ip_ood),3,),)
user = var.ood_user
private_key = file(var.ssh_private_key)
}
}
output "ssh_host"{
value = format(var.host_prefix,element(split(".", var.floating_ip_ood),3,),)
}
\ No newline at end of file
variable "admin-state-up" { # These are the defined variables used in all the modules. They are referenced in the modules, and defined here.
description = "whether admin state in enabled. defualt is true" # This is where you would change any variable values.
default = true
} # variables for networks and router
variable "admin_state_up" {default = true}
variable "enable-dhcp" { variable "enable_dhcp" {default = true}
description = "whether dhcp in enabled. defualt is true"
default = true # variable for floating-ip - also used in exrernal network creation
} variable "public_network_name" {default = "bright-external-flat-externalnet"}
variable "external-net" { # variables for keypair module
default = "dmznet" variable "keypair_name" {default = "os-gen-keypair"}
} variable "ssh_public_key" {default = "~/.ssh/id_rsa.pub"}
variable "external-subnet" { # variables for instance modules
default = "dmzsubnet" variable "ohpc_instance_name" {default = "ohpc"}
} variable "ood_instance_name" { default = "ood"}
variable "image_ohpc" {default = "CentOS-7-x86_64-GenericCloud-1905"}
variable "flavor" { variable "image_ood" {default = "CentOS-7-x86_64-GenericCloud-1905"}
default = "m1.medium" variable "flavor" {default = "m1.medium"}
} variable "internal_network" {default = "clusternet"}
variable "external_network" {default = "dmznet"}
variable "host-prefix" { variable "host_prefix" {default = "164.111.161.%s"}
description = "prefix of host id." variable "ohpc_user" {default = "centos"}
default = "164.111.161.%s" variable "ood_user" {default = "centos"}
} variable "ssh_private_key" {default = "~/.ssh/id_rsa"}
variable "internal-net" {
default = "clusternet" # variables for node creation module
} variable "image_compute" {default = "CentOS-7-x86_64-GenericCloud-1905"}
variable "compute_node_count" {default = 2}
variable "internal-subnet" {
default = "clustersubnet"
}
variable "image_ohpc" {
default = "CentOS-7-x86_64-GenericCloud-1905"
}
variable "image_ood" {
default = "CentOS-7-x86_64-GenericCloud-1905"
}
variable "image_compute" {
default = "CentOS-7-x86_64-GenericCloud-1905"
}
variable "compute_node_count" {
default = 2
}
variable "keypair-name" {
default = "os-gen-keypair"
}
variable "ohpc-instance-name" {
default = "ohpc"
}
variable "ohpc-user" {
default = "centos"
}
variable "ood-instance-name" {
default = "ood"
}
variable "ood-user" {
default = "centos"
}
variable "public-network-name" {
default = "bright-external-flat-externalnet"
}
variable "router" {
default = "borderrouter"
}
variable "ssh-private-key" {
description = "Path to file containing private key"
default = "~/.ssh/id_rsa"
}
variable "ssh-public-key" {
description = "Path to file containing public key"
default = "~/.ssh/id_rsa.pub"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment