terraform-coop / terraform-provider-foreman

Terraform provider for Foreman
https://registry.terraform.io/providers/terraform-coop/foreman
Mozilla Public License 2.0
35 stars 32 forks source link

Remove build flag and re-introduce provision method attribute #122

Closed bitkeks closed 1 year ago

bitkeks commented 1 year ago

The build flag was introduced as the replacement for the "method" attribute. Earlier, the method could be set to "build" or "image", which is then used by Foreman. The initial creation of a host also needed the build flag to be set in the API request to signal Foreman that it should create the machine.

This lead to the problem that Terraform configs containing "build = true" would reset the build flag in Foreman even for existing, already built machines, resetting them at the next boot. This is dangerous and should be avoided.

The commit removes the build flag completely from Terraform and uses the "provision_method" attribute from the Foreman API (again).

We will have to find an additional attribute to support the use case of "enforced rebuild" that originally lead to the switch.

The "managed" flag is not changed.

Refs #40 Refs #68 Refs #106

togoschi commented 1 year ago

My tests with self-compiled binary were successful either with network-based or image-based provisioning method (running against foreman v3.6.1)

My test code (image-based provisioning example with vSphere provider)

data "foreman_image" "ubuntu" {
  name ="Ubuntu 22.04 LTS"
  compute_resource_id = data.foreman_computeresource.vsphere.id
}

// [Foreman Hosts]--------------------------
resource "foreman_host" "image_based_machine" {
  count               = var.hosts.count
  shortname       = format(var.hosts.hostname_format, count.index + 1)
  provision_method = "image"
  image_id         = data.foreman_image.ubuntu.id

  compute_attributes = format(<<EOF
    {
        "image_id": "%s"
    }
  EOF
  ,
  data.foreman_image.ubuntu.uuid)

  hostgroup_id        = foreman_hostgroup.ubuntu.id
  compute_resource_id = data.foreman_computeresource.vsphere.id
}