main.tf 2.68 KB
Newer Older
Ryan Randles Jones's avatar
Ryan Randles Jones committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

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

# 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}"
  key_pair        = "${var.key_pair}"
  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"
    ]
  }
}

# creates details for the instance using variables defined in vars.tf
resource "openstack_compute_instance_v2" "ohpc" {

  name            = "ohpc"
  image_name      = "${var.image}"
  flavor_name     = "${var.flavor}"
  key_pair        = "${var.key_pair}"
  security_groups = ["default"]

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