@@ -125,8 +125,9 @@ data "openstack_images_image_v2" "my_image" {
125125 os_version = "24.04"
126126 os_purpose = "generic"
127127 }
128- # sort_key = "name"
129- # sort_direction = "desc"
128+ # sort = "name:desc,created_at:desc"
129+ # sort_key = "name"
130+ # sort_direction = "desc"
130131}
131132
132133# Use the selected image
@@ -138,11 +139,63 @@ resource "openstack_compute_instance_v2" "instance" {
138139
139140This will find the most recent image wtih the ` os_ ` variables set to ` ubuntu ` , ` 24.04 ` , ` generic ` .
140141Note 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.
142+ date at the same time; the only option to deal with several matches is to tell opentofu's
143+ openstack provider to return the latest (the one with the newest ` created_at ` date).
144144
145145An example can be found in [ find_img.tf] ( find_img.tf ) . Call it with ` tofu apply -auto-approve `
146146(after you ran ` tofu init ` in this directory once).
147147
148- The fallback to name matching is harder.
148+ The fallback to name matching for clouds that don't have ` os_purpose ` yet is harder.
149+
150+ We use an external program, the python script from before to select the right image and just create
151+ a little wrapper around it: [ find_img2.py] ( find_img2.py ) . The HCL then looks like this:
152+
153+ ``` hcl
154+ # Call python find_img.py to find best image
155+ data "external" "my_image2" {
156+ program = ["python3", "${path.module}/find_img2.py"]
157+
158+ query = {
159+ os_distro = "${var.os_distro2}"
160+ os_version = "${var.os_version2}"
161+ os_purpose = "${var.os_purpose2}"
162+ }
163+ }
164+
165+ # Output the results for inspection
166+ output "selected_image2" {
167+ value = {
168+ id = data.external.my_image2.result.image_id
169+ name = data.external.my_image2.result.image_name
170+ }
171+ }
172+ ```
173+
174+ Note that I have appended a ` 2 ` to the variable names, so they don't clash in case you have the
175+ original example in the same directory.
176+
177+ ## heat
178+
179+ I did not find a good way to select an image based on its properties in heat.
180+ Obviously, you can use the python (or shell) script above and pass the image name
181+ or ID as a parameter when invoking heat.
182+
183+ ``` yaml
184+ heat_template_version : 2018-08-31
185+
186+ parameters :
187+ image :
188+ type : string
189+ description : Image ID or name
190+ constraints :
191+ - custom_constraint : glance.image
192+
193+ resources :
194+ my_instance :
195+ type : OS::Nova::Server
196+ properties :
197+ image : { get_param: image }
198+ # ... other properties
199+ ```
200+
201+ and call ` openstack stack create --parameter image=$ID $TEMPLATE $STACKNAME ` .
0 commit comments