Skip to content

Commit e8f6f13

Browse files
committed
ENH - os_builders: Update build.pkr.hcl with env
Using an input variable and conditional check we can set either the dev or prod network ID at runtime. Rather than the user edit the file or input it on the command line. Before users had to edit build.pkr.hcl and update the network ID and set it to either prod or dev. Now users can just provide the flag --var env=prod when running packer build and it will use the dev or prod ID depending on the value of the variable. The variable defaults to dev if not specified as a saftey catch.
1 parent 1b82bf4 commit e8f6f13

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

os_builders/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ The following steps assume you have set up your VM correctly as in the previous
9191

9292
2. Initiate Packer image build for all images
9393
```shell
94-
packer build build.pkr.hcl
94+
packer build --var env=dev build.pkr.hcl
9595
```
9696
3. Intiate Packer image build for multiple images
9797
```shell
9898
# Replace image-name with the actual image name. "openstack." is required
99-
packer build -only <openstack.image-name>,<openstack.image-name>... build.pkr.hcl
99+
packer build --var env=dev -only <openstack.image-name>,<openstack.image-name>... build.pkr.hcl
100100
# e.g.
101-
packer build -only openstack.ubuntu-jammy-22.04-nogui,openstack.rocky-8-nogui build.pkr.hcl
101+
packer build --var env=dev -only openstack.ubuntu-jammy-22.04-nogui,openstack.rocky-8-nogui build.pkr.hcl
102102
```
103103
4. Initiate Packer image build for a single image
104104
```shell
105105
# Replace image-name with the actual image name. "openstack." is required
106-
packer build -only <openstack.image-name> build.pkr.hcl
106+
packer build --var env=dev -only <openstack.image-name> build.pkr.hcl
107107
# e.g.
108-
packer build -only openstack.ubuntu-jammy-22.04-nogui build.pkr.hcl
108+
packer build --var env=dev -only openstack.ubuntu-jammy-22.04-nogui build.pkr.hcl
109109
```
110110
5. Once the build completes successfully you should get a UUID of the new image
111111

os_builders/build.pkr.hcl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ packer {
1111
}
1212
}
1313

14+
variable "env" {
15+
type = string
16+
default = "dev"
17+
}
18+
1419
locals {
20+
env_network_id = var.env == "prod" ? "5be315b7-7ebd-4254-97fe-18c1df501538" : "fa2f5ebe-d0e0-4465-9637-e9461de443f1"
1521
date_suffix = "${formatdate("YYYY-MM-DD", timestamp())}"
1622
metadata = {
1723
"hw_machine_type" : "q35",
@@ -43,7 +49,7 @@ source "openstack" "builder" {
4349
domain_name = "Default"
4450
flavor = "l3.imagecreate"
4551
security_groups = ["default"]
46-
networks = ["fa2f5ebe-d0e0-4465-9637-e9461de443f1"] # Dev OpenStack Network ID
52+
networks = ["${local.env_network_id}"]
4753
image_visibility = "private"
4854
ssh_timeout = "20m"
4955
image_min_disk = "20"

0 commit comments

Comments
 (0)