old_main 5.14 KB
Newer Older
Ryan Randles Jones's avatar
Ryan Randles Jones committed
1
2
3
4
5
# creates public and private keypair
resource "openstack_compute_keypair_v2" "test-keypair" {
  name = "my-keypair"
}

Ryan Jones's avatar
Ryan Jones committed
6
# creates dmznet
Ryan Randles Jones's avatar
Ryan Randles Jones committed
7
resource "openstack_networking_network_v2" "terraform" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
8
  name           = "dmznet"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
9
  admin_state_up = "true"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
10
11
}

Ryan Jones's avatar
Ryan Jones committed
12
13
# 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) 
Ryan Randles Jones's avatar
Ryan Randles Jones committed
14
resource "openstack_networking_subnet_v2" "terraform" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
15
  name            = "dmzsubnet"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
16
  network_id      = "${openstack_networking_network_v2.terraform.id}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
17
  cidr            = "192.168.100.0/24"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
18
  ip_version      = 4
Ryan Randles Jones's avatar
Ryan Randles Jones committed
19
  dns_nameservers = ["8.8.8.8"]
Ryan Randles Jones's avatar
Ryan Randles Jones committed
20
21
}

Ryan Jones's avatar
Ryan Jones committed
22
# defines the router borderrouter using floating ip defined in datasources.tf to create the external network id
Ryan Randles Jones's avatar
Ryan Randles Jones committed
23
resource "openstack_networking_router_v2" "terraform" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
24
  name                = "borderrouter"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
25
26
27
28
  admin_state_up      = "true"
  external_network_id = "${data.openstack_networking_network_v2.terraform.id}"
}

Ryan Jones's avatar
Ryan Jones committed
29
# creates the router and subnet id using info defined above 
Ryan Randles Jones's avatar
Ryan Randles Jones committed
30
31
32
33
34
resource "openstack_networking_router_interface_v2" "terraform" {
  router_id = "${openstack_networking_router_v2.terraform.id}"
  subnet_id = "${openstack_networking_subnet_v2.terraform.id}"
}

Ryan Jones's avatar
Ryan Jones committed
35
# creates a security group
Ryan Randles Jones's avatar
Ryan Randles Jones committed
36
37
38
39
40
resource "openstack_networking_secgroup_v2" "terraform" {
  name        = "terraform"
  description = "Security group for the Terraform example instances"
}

Ryan Jones's avatar
Ryan Jones committed
41
# creates details of security group fir port 22 and creates security group id
Ryan Randles Jones's avatar
Ryan Randles Jones committed
42
43
44
45
46
47
48
49
50
51
resource "openstack_networking_secgroup_rule_v2" "terraform_22" {
  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.terraform.id}"
}

Ryan Jones's avatar
Ryan Jones committed
52
# creates details of security group fir port 80 and creates security group id
Ryan Randles Jones's avatar
Ryan Randles Jones committed
53
54
55
56
57
58
59
60
61
62
resource "openstack_networking_secgroup_rule_v2" "terraform_80" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "tcp"
  port_range_min    = 80
  port_range_max    = 80
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = "${openstack_networking_secgroup_v2.terraform.id}"
}

Ryan Jones's avatar
Ryan Jones committed
63
# creates details of security group fir protocol icmp and creates security group id
Ryan Randles Jones's avatar
Ryan Randles Jones committed
64
65
66
67
68
69
70
71
resource "openstack_networking_secgroup_rule_v2" "terraform" {
  direction         = "ingress"
  ethertype         = "IPv4"
  protocol          = "icmp"
  remote_ip_prefix  = "0.0.0.0/0"
  security_group_id = "${openstack_networking_secgroup_v2.terraform.id}"
}

Ryan Jones's avatar
Ryan Jones committed
72
# defines where floating ip will come from using variable from vars.tf
Ryan Randles Jones's avatar
Ryan Randles Jones committed
73
resource "openstack_networking_floatingip_v2" "terraform" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
74
75
76
  pool = "${var.pool}"
}

Ryan Jones's avatar
Ryan Jones committed
77
# creates details for the instance using variables defined in vars.tf and resource for security groups
Ryan Randles Jones's avatar
Ryan Randles Jones committed
78
resource "openstack_compute_instance_v2" "terraform" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
79
  name            = "ohpc"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
80
81
  image_name      = "${var.image}"
  flavor_name     = "${var.flavor}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
82
  key_pair        = "${openstack_compute_keypair_v2.test-keypair.name}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
83
84
  security_groups = ["${openstack_networking_secgroup_v2.terraform.name}"]

Ryan Jones's avatar
Ryan Jones committed
85
# defines the instance id using info from datasources.tf
Ryan Randles Jones's avatar
Ryan Randles Jones committed
86
87
  network {
    uuid = "${openstack_networking_network_v2.terraform.id}"
Ryan Jones's avatar
Ryan Jones committed
88
 }
Ryan Randles Jones's avatar
Ryan Randles Jones committed
89
90
}

Ryan Jones's avatar
Ryan Jones committed
91
# associates floating ip with the instance
Ryan Randles Jones's avatar
Ryan Randles Jones committed
92
93
94
95
resource "openstack_compute_floatingip_associate_v2" "terraform" {
  floating_ip = "${openstack_networking_floatingip_v2.terraform.address}"
  instance_id = "${openstack_compute_instance_v2.terraform.id}"
  }
Ryan Randles Jones's avatar
Ryan Randles Jones committed
96
97

# creates clusternet
Ryan Jones's avatar
Ryan Jones committed
98
resource "openstack_networking_network_v2" "terraform2" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
99
  name           = "clusternet"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
100
101
102
  admin_state_up = "true"
}

Ryan Jones's avatar
Ryan Jones committed
103
# creates clustersubnet
Ryan Randles Jones's avatar
Ryan Randles Jones committed
104
# cidr is the subnet range (that subnet range and dns nameservers from the network create file in feat-openstack) 
Ryan Jones's avatar
Ryan Jones committed
105
resource "openstack_networking_subnet_v2" "terraform2" {
Ryan Randles Jones's avatar
Ryan Randles Jones committed
106
  name            = "clustersubnet"
Ryan Jones's avatar
Ryan Jones committed
107
  network_id      = "${openstack_networking_network_v2.terraform2.id}"
Ryan Randles Jones's avatar
Ryan Randles Jones committed
108
109
  cidr            = "10.1.1.0/24"
  ip_version      = 4
Ryan Jones's avatar
Ryan Jones committed
110
111
112
113
114
}

# defines the instance and network id using info defined above
resource "openstack_compute_interface_attach_v2" "terraform2" {
  instance_id = "${openstack_compute_instance_v2.terraform.id}" 
Ryan Jones's avatar
Ryan Jones committed
115
  network_id  = "${openstack_networking_network_v2.terraform2.id}"  
Ryan Jones's avatar
Ryan Jones committed
116
}
Ryan Randles Jones's avatar
Ryan Randles Jones committed
117

Ryan Randles Jones's avatar
Ryan Randles Jones committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  provisioner "remote-exec" {
    inline = [
    "hostnamectl set-hostname ohpc",
    "yum install -y epel-release",
    "yum install -y ansible git vim bash-completion",
    "yum install -y NetworkManager",
    "systemctl restart NetworkManager",
    "nmcli con mod 'Wired connection 1' connection.id 'eth1'",
    ]

    connection {
        host        = "${self.ipv4_address}" # The `self` variable is like `this` in many programming languages
        type        = "ssh"                  # in this case, `self` is the resource (the server).
        user        = "root"
        private_key = "${file("~/.ssh/id_rsa")}"
    }
  }

  provisioner "local-exec" {
    environment {
    PUBLIC_IP  = "${self.ipv4_address}"
    PRIVATE_IP = "${self.ipv4_address_private}"
    }

    working_dir = "$HOME/terraform-first-instance/terraform-openstack/CRI_XCBC/"
    command     = "ansible-playbook -c local -i hosts -l `hostname` site.yaml -b,"
    }
}