Skip to content

Commit d06f048

Browse files
authored
Fixed names and added volume example (terraform-provider-openstack#892)
* Fixed invalid output name * Fixed reserved variable name and added output * Added template to attach,format, and mount volume * Removed wget command from provisioner * Changed units to instance_count
1 parent e9e493c commit d06f048

11 files changed

Lines changed: 187 additions & 14 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Attach, format, and mount a Block Storage Volume to an Instance
2+
3+
This provides a template for attaching, formating, and mounting a Block Storage Volume to an Instance.
4+
5+
## Usage
6+
7+
Download and install [Terraform](https://www.terraform.io/downloads.html):
8+
9+
```sh
10+
wget -P /tmp/ https://releases.hashicorp.com/terraform/0.12.9/terraform_0.12.9_linux_amd64.zip
11+
unzip /tmp/terraform_0.12.9_linux_amd64.zip
12+
sudo mv terraform /usr/local/bin/
13+
terraform --version
14+
```
15+
16+
Log in to the OpenStack dashboard, choose the project for which you want to download the OpenStack RC file, and run the following commands:
17+
18+
```sh
19+
source ~/Downloads/PROJECT-openrc.sh
20+
Please enter your OpenStack Password for project PROJECT as user username:
21+
```
22+
23+
### Initialize providers
24+
25+
```sh
26+
terraform init
27+
```
28+
29+
### Generate an execution plan
30+
31+
```sh
32+
terraform plan
33+
# or with specific variables
34+
terraform plan -var 'pool=gateway' \
35+
-var 'flavor=m02.c02.d20' \
36+
-var 'volume_size=3' \
37+
-var 'network_name=my-network' \
38+
-var 'ssh_key_file=./id_rsa_os'
39+
```
40+
41+
### Apply the plan
42+
43+
```sh
44+
terraform apply -auto-approve
45+
# or with specific variables
46+
terraform apply -auto-approve \
47+
-var 'pool=gateway' \
48+
-var 'flavor=m02.c02.d20' \
49+
-var 'volume_size=3' \
50+
-var 'network_name=my-network' \
51+
-var 'ssh_key_file=./id_rsa_os'
52+
```
53+
54+
Upon completion,the instances floating IP can be viewed in `instance_ip.txt`.
55+
56+
### Destroy
57+
58+
> watch the `-force` will not ask for any confirmation.
59+
60+
```sh
61+
terraform destroy -force
62+
# or with specific variables
63+
terraform destroy -force \
64+
-var 'pool=gateway' \
65+
-var 'flavor=m02.c02.d20' \
66+
-var 'volume_size=3' \
67+
-var 'network_name=my-network' \
68+
-var 'ssh_key_file=./id_rsa_os'
69+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "openstack_compute_keypair_v2" "terraform" {
2+
name = "terraform"
3+
public_key = "${file("${var.ssh_key_file}.pub")}"
4+
}
5+
6+
resource "openstack_compute_instance_v2" "my_instance" {
7+
name = "my_instance"
8+
image_name = "${var.image}"
9+
flavor_name = "${var.flavor}"
10+
key_pair = "${openstack_compute_keypair_v2.terraform.name}"
11+
security_groups = ["default"]
12+
network {
13+
name = "${var.network_name}"
14+
}
15+
}
16+
17+
resource "openstack_networking_floatingip_v2" "fip" {
18+
pool = "${var.pool}"
19+
}
20+
21+
resource "openstack_compute_floatingip_associate_v2" "fip" {
22+
instance_id = "${openstack_compute_instance_v2.my_instance.id}"
23+
floating_ip = "${openstack_networking_floatingip_v2.fip.address}"
24+
connection {
25+
host = "${openstack_networking_floatingip_v2.fip.address}"
26+
user = "${var.ssh_user_name}"
27+
private_key = "${file(var.ssh_key_file)}"
28+
}
29+
30+
provisioner "local-exec" {
31+
command = "echo ${openstack_networking_floatingip_v2.fip.address} > instance_ip.txt"
32+
}
33+
34+
provisioner "remote-exec" {
35+
inline = [
36+
"sudo mkfs.ext4 ${openstack_compute_volume_attach_v2.attached.device}",
37+
"sudo mkdir /mnt/volume",
38+
"sudo mount ${openstack_compute_volume_attach_v2.attached.device} /mnt/volume",
39+
"sudo df -h /mnt/volume",
40+
]
41+
}
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
export OS_AUTH_URL=http://KEYSTONE.ENDPOINT.URL:5000/v3
4+
export OS_PROJECT_ID="42..."
5+
export OS_PROJECT_NAME="multivm-project"
6+
export OS_USER_DOMAIN_NAME="mycloud"
7+
export OS_PROJECT_DOMAIN_ID="23..."
8+
export OS_USERNAME="$USER"
9+
export OS_PASSWORD=$OS_PASSWORD_INPUT
10+
export OS_REGION_NAME="MyRegion"
11+
export OS_INTERFACE=public
12+
export OS_IDENTITY_API_VERSION=3
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "floating_ip" {
2+
value = "${openstack_networking_floatingip_v2.fip.address}"
3+
}
4+
5+
output "volume_devices" {
6+
value = "${openstack_compute_volume_attach_v2.attached.device}"
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
variable "image" {
2+
default = "Ubuntu 18.04"
3+
}
4+
5+
variable "flavor" {
6+
default = "m1.small"
7+
}
8+
9+
variable "ssh_key_file" {
10+
default = "~/.ssh/id_rsa"
11+
}
12+
variable "volume_size" {
13+
default = 1
14+
}
15+
16+
variable "ssh_user_name" {
17+
default = "ubuntu"
18+
}
19+
20+
variable "pool" {
21+
default = "public"
22+
}
23+
24+
variable "network_name" {
25+
default = "my_network"
26+
}
27+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "openstack_blockstorage_volume_v2" "volume_1" {
2+
name = "volume_1"
3+
size = "${var.volume_size}"
4+
}
5+
6+
resource "openstack_compute_volume_attach_v2" "attached" {
7+
instance_id = "${openstack_compute_instance_v2.my_instance.id}"
8+
volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}"
9+
# Prevent re-creation
10+
# lifecycle {
11+
# ignore_changes = ["volume_id", "instance_id"]
12+
# }
13+
}

‎examples/multiple-vm-with-floating-ip/Readme.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ terraform plan
3030
# or with specific variables
3131
terraform plan -var 'pool=gateway' \
3232
-var 'flavor=m02.c02.d20' \
33-
-var 'count=3' \
33+
-var 'instance_count=3' \
3434
-var 'network_name=my-network' \
3535
-var 'ssh_key_file=./id_rsa_os'
3636
```
@@ -43,7 +43,7 @@ terraform apply -auto-approve
4343
terraform apply -auto-approve \
4444
-var 'pool=gateway' \
4545
-var 'flavor=m02.c02.d20' \
46-
-var 'count=3' \
46+
-var 'instance_count=3' \
4747
-var 'network_name=my-network' \
4848
-var 'ssh_key_file=./id_rsa_os'
4949
```
@@ -59,7 +59,7 @@ terraform destroy -force
5959
terraform destroy -force \
6060
-var 'pool=gateway' \
6161
-var 'flavor=m02.c02.d20' \
62-
-var 'count=3' \
62+
-var 'instance_count=3' \
6363
-var 'network_name=my-network' \
6464
-var 'ssh_key_file=./id_rsa_os'
6565
```

‎examples/multiple-vm-with-floating-ip/main.tf‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ resource "openstack_compute_keypair_v2" "terraform" {
44
}
55

66
resource "openstack_compute_instance_v2" "multi" {
7-
count = "${var.count}"
8-
name = "${format("${var.instance_prefix}-%02d", count.index+1)}"
9-
image_name = "${var.image}"
10-
flavor_name = "${var.flavor}"
11-
key_pair = "${openstack_compute_keypair_v2.terraform.name}"
7+
count = "${var.instance_count}"
8+
name = "${format("${var.instance_prefix}-%02d", count.index + 1)}"
9+
image_name = "${var.image}"
10+
flavor_name = "${var.flavor}"
11+
key_pair = "${openstack_compute_keypair_v2.terraform.name}"
1212
security_groups = ["default"]
1313
network {
1414
name = "${var.network_name}"
1515
}
1616
}
1717

1818
resource "openstack_networking_floatingip_v2" "fip" {
19-
count = "${var.count}"
20-
pool = "${var.pool}"
19+
count = "${var.instance_count}"
20+
pool = "${var.pool}"
2121
}
2222

2323
resource "openstack_compute_floatingip_associate_v2" "fip" {
24-
count = "${var.count}"
24+
count = "${var.instance_count}"
2525
instance_id = "${openstack_compute_instance_v2.multi.*.id[count.index]}"
2626
floating_ip = "${openstack_networking_floatingip_v2.fip.*.address[count.index]}"
2727
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "floating_ip_addresses" {
2+
value = "${openstack_networking_floatingip_v2.fip.*.address}"
3+
}

‎examples/multiple-vm-with-floating-ip/variables.tf‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ variable "pool" {
1818
default = "public"
1919
}
2020

21-
variable "count" {
21+
variable "instance_count " {
2222
default = 2
2323
}
2424

0 commit comments

Comments
 (0)