Skip to content
Snippets Groups Projects
main.tf 4.01 KiB
Newer Older
Ryan Randles Jones's avatar
Ryan Randles Jones committed
# creates public and private keypair
resource "openstack_compute_keypair_v2" "test-keypair" {
  name = "my-keypair"
}

# creates dmznet
resource "openstack_networking_network_v2" "ohpc" {
  name           = "dmznet"
  admin_state_up = "true"
}
Ryan Randles Jones's avatar
Ryan Randles Jones committed
# creates dmzsubnet using the floating ip defined in datasources.tf to get the network id
# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack) 
resource "openstack_networking_subnet_v2" "terraform" {
  name            = "dmzsubnet"
  network_id      = "${openstack_networking_network_v2.ohpc.id}"
  cidr            = "192.168.100.0/24"
  ip_version      = 4
  dns_nameservers = ["8.8.8.8"]
}
Ryan Randles Jones's avatar
Ryan Randles Jones committed
# defines the router borderrouter using floating ip defined in datasources.tf to create the external network id
resource "openstack_networking_router_v2" "ohpc" {
  name                = "borderrouter"
  admin_state_up      = "true"
  external_network_id = "${data.openstack_networking_network_v2.ohpc.id}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
# creates the router and subnet id using info defined above 
resource "openstack_networking_router_interface_v2" "ohpc" {
  router_id = "${openstack_networking_router_v2.ohpc.id}"
  subnet_id = "${openstack_networking_subnet_v2.ohpc.id}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
}
Ryan Randles Jones's avatar
Ryan Randles Jones committed

# defines where floating ip will come from using variable from vars.tf
resource "openstack_networking_floatingip_v2" "ohpc" {
  pool = "${var.pool}"
}

Ryan Randles Jones's avatar
Ryan Randles Jones committed
# creates clusternet
resource "openstack_networking_network_v2" "ohpc2" {
  name           = "clusternet"
  admin_state_up = "true"
}

# 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" "ohpc2" {
  name            = "clustersubnet"
  network_id      = "${openstack_networking_network_v2.ohpc2.id}"
  cidr            = "10.1.1.0/24"
  ip_version      = 4
}


Ryan Randles Jones's avatar
Ryan Randles Jones committed
# creates details for the instance using variables defined in vars.tf and resource for security groups
resource "openstack_compute_instance_v2" "ohpc" {

  name            = "ohpc"
  image_name      = "${var.image}"
  flavor_name     = "${var.flavor}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
  key_pair        = "${openstack_compute_keypair_v2.test-keypair.name}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
  security_groups = ["default"]

# defines the networks of the instance
  network {
    name = "dmznet"
  }
  network {
    name = "clusternet"
  }
}

# associates floating ip with the instance
resource "openstack_compute_floatingip_associate_v2" "ohpc" {
  floating_ip = "${openstack_networking_floatingip_v2.ohpc.address}"
  instance_id = "${openstack_compute_instance_v2.ohpc.id}"

  provisioner "remote-exec" {
    connection {
      host    =  "${format("164.111.161.%s", element(split(".", openstack_networking_floatingip_v2.ohpc.address),3))}"
      #host        = "${openstack_networking_floatingip_v2.ohpc.address}"
      user        = "centos"
      private_key = "${file(var.ssh_key_private)}"
    }

    inline = [
      "sudo mkdir -p /CRI_XCBC && sudo chown centos: /CRI_XCBC",
      "sudo yum install -y epel-release",
      "sudo yum install -y ansible git vim bash-completion",
      "sudo yum install -y NetworkManager",
      "sudo systemctl restart NetworkManager",
      "sudo nmcli con mod 'Wired connection 1' connection.id 'eth1'"
    ]
  }
  provisioner "file" {
    source      = "CRI_XCBC/"
    destination = "/CRI_XCBC/"
    connection {
      host    =  "${format("164.111.161.%s", element(split(".", openstack_networking_floatingip_v2.ohpc.address),3))}"
      #host        = "${openstack_networking_floatingip_v2.terraform.address}"
      user        = "centos"
      private_key = "${file(var.ssh_key_private)}"
    }
  }
  provisioner "remote-exec" {
    connection {
      host    =  "${format("164.111.161.%s", element(split(".", openstack_networking_floatingip_v2.ohpc.address),3))}"
      #host        = "${openstack_networking_floatingip_v2.ohpc.address}"
      user        = "centos"
      private_key = "${file(var.ssh_key_private)}"
    }
    inline = [
      "sudo ansible-playbook -c local -i /CRI_XCBC/hosts -l ohpc /CRI_XCBC/site.yaml -b"
    ]
  }
}