ionos-cloud / terraform-provider-profitbricks

Terraform ProfitBricks provider
https://www.terraform.io/docs/providers/profitbricks/
Mozilla Public License 2.0
3 stars 16 forks source link

Error "Cannot include a null value in a string template" during plan-phase #70

Open soenkeliebau opened 4 years ago

soenkeliebau commented 4 years ago

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Terraform v0.12.28

Affected Resource(s)

The issue turned up in the combination of the following two resources:

Terraform Configuration Files

main.tf

provider "profitbricks" {
  username = "xxx"
  password = "xxx"
}

resource "profitbricks_datacenter" "stackable-demo" {
  name = "demo datacenter 02"
  location = "de/fra"
  description = "Provisioned via terraform for ionos demo"
}

// Internet facing lan
resource "profitbricks_lan" "external" {
  name = "External Network"
  datacenter_id = profitbricks_datacenter.stackable-demo.id
  public = true
}

resource "profitbricks_server" "nat" {
  name = "nat"
  datacenter_id = profitbricks_datacenter.stackable-demo.id
  cores = 2
  ram = 1024
  availability_zone = "ZONE_1"
  cpu_family = "AMD_OPTERON"

  image_name = "CentOS-7-server-2020-06-01"
  ssh_key_path = ["xxx"]

  volume {
    name = "nat-storage"

    size = 15
    disk_type = "HDD"
  }

  nic {
    lan = profitbricks_lan.external.id
    dhcp = true
    firewall_active = false
  }
}

# generate inventory file for Ansible
resource "local_file" "hosts_cfg" {
  content = templatefile("${path.module}/template/hosts.tpl",
  {
    nat = profitbricks_server.nat
  }
  )
  filename = "../inventory-generated/demo_generated.inv"
}

hosts.tpl

${nat.nic[0].ip}

Debug Output

https://gist.github.com/soenkeliebau/2e7d4a3f0d91ec5a3d4ae42f4e7c30f8

Expected Behavior

I expected terraform to provision the resources and then fill the template with proper values after the apply finished.

Actual Behavior

Terraform errored out during the plan phase with the following error message:

Call to function "templatefile" failed: ./template/hosts.tpl:1,3-16: Invalid template interpolation value; The expression result is null. Cannot include a null value in a string template..

If I remove this expression from the hosts.tpl file when starting terraform and insert it again, before the command is finished, the proper value is inserted into the template.

To me this looks a bit like the provisioner is setting null instead of unknown as value for some properties during the plan phase, which causes the templatefile to evaluate these and create the error shown.

Replacing ${nat.nic[0].ip} with ${nat.nic[0].ip == null ? "null" : nat.nic[0].ip} solves the issue as well, but seems to me like a workaround rather than a fix.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
jbuchhammer commented 3 years ago

Hi @soenkeliebau, I guess, the null comes from accessing the not existing nic list. I could also reproduce the error.

However, there's no need to workaround. You just have to use

${nat.primary_ip}

in the template file.

FYI the versions I used:

Terraform v0.12.25
+ provider.local v1.4.0
+ provider.profitbricks v1.5.5