Skip to content

Commit 520d7fd

Browse files
committed
opentofu/HCL, first part.
Signed-off-by: Kurt Garloff <[email protected]>
1 parent 5c703f3 commit 520d7fd

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env tofu apply -auto-approve
2+
# Pass variables with -var os_purpose=minimal
3+
4+
variable "os_distro" {
5+
type = string
6+
default = "ubuntu"
7+
}
8+
9+
variable "os_version" {
10+
type = string
11+
default = "24.04"
12+
}
13+
14+
variable "os_purpose" {
15+
type = string
16+
default = "generic"
17+
}
18+
19+
terraform {
20+
required_providers {
21+
openstack = {
22+
source = "terraform-provider-openstack/openstack"
23+
version = "~> 1.54.0"
24+
}
25+
}
26+
}
27+
28+
provider "openstack" {
29+
# Your cloud config (or use environment variables OS_CLOUD)
30+
}
31+
32+
data "openstack_images_image_v2" "my_image" {
33+
most_recent = true
34+
35+
properties = {
36+
os_distro = "${var.os_distro}"
37+
os_version = "${var.os_version}"
38+
os_purpose = "${var.os_purpose}"
39+
}
40+
}
41+
42+
# Output the results for inspection
43+
output "selected_image_id" {
44+
value = data.openstack_images_image_v2.my_image.id
45+
}
46+
47+
output "selected_image_name" {
48+
value = data.openstack_images_image_v2.my_image.name
49+
}
50+
51+
output "selected_image_created_at" {
52+
value = data.openstack_images_image_v2.my_image.created_at
53+
}
54+
55+
output "selected_image_properties" {
56+
value = {
57+
os_distro = data.openstack_images_image_v2.my_image.properties.os_distro
58+
os_version = data.openstack_images_image_v2.my_image.properties.os_version
59+
os_purpose = data.openstack_images_image_v2.my_image.properties.os_purpose
60+
}
61+
}
62+
63+
output "selected_image_tags" {
64+
value = data.openstack_images_image_v2.my_image.tags
65+
}
66+

user-docs/usage-hints/find-image/index.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ We have to expect several matches here and need some heuristic to find the
8282
right image, preferrably the one matching the old naming convention.
8383

8484
Full code that does this is available in [find_img.py](find_img.py).
85+
The script assume that you have set your `OS_CLOUD` environment variable
86+
and have configured working `clouds.yaml` and `secure.yaml`.
8587
Feel free to copy, I deliberately put this under MIT license.
8688

8789
## Identifying the image with OpenStack CLI
@@ -108,6 +110,39 @@ somewhat similar (but not identical) to the python code.
108110

109111
Full code that does this is available in [find_img.sh](find_img.sh).
110112

111-
## Terraform / opentofu
113+
## opentofu / terraform
114+
115+
With opentofu (or Hashicorp's terraform is you still use it), identifying
116+
the image in an HCL recipe looks like this:
117+
118+
```hcl
119+
# Find the image
120+
data "openstack_images_image_v2" "my_image" {
121+
most_recent = true
122+
123+
properties = {
124+
os_distro = "ubuntu"
125+
os_version = "24.04"
126+
os_purpose = "generic"
127+
}
128+
# sort_key = "name"
129+
# sort_direction = "desc"
130+
}
131+
132+
# Use the selected image
133+
resource "openstack_compute_instance_v2" "instance" {
134+
image_id = data.openstack_images_image_v2.my_image.id
135+
...
136+
}
137+
```
138+
139+
This will find the most recent image wtih the `os_` variables set to `ubuntu`, `24.04`, `generic`.
140+
Note that unlike the python and shell examples, we can not easily sort for name and creation
141+
date at the same time; the name sorting is thus commented out here. To use name sorting you can
142+
enable it — using it and then selecting the latest if the name is identical requires some
143+
HCL magic using `locals` and `reverse(sort(...))` calls or calling an external program.
144+
145+
An example can be found in [find_img.tf](find_img.tf). Call it with `tofu apply -auto-approve`
146+
(after you ran `tofu init` in this directory once).
112147

113-
TBW
148+
The fallback to name matching is harder.

0 commit comments

Comments
 (0)