Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Chef provisioner fails when using "for_each" and "each" for resources #33

Open
kszlachcikowski opened this issue Aug 2, 2019 · 2 comments
Labels

Comments

@kszlachcikowski
Copy link

kszlachcikowski commented Aug 2, 2019

Hi,

There seems to be an issue with the chef provisioner when generating resources using "for_each" (new in terraform 0.12.6). Whenever the "each" variable is used within the chef provisioner block of the resource to dynamically fill out attributes (eg. "node_name") terraform fails during the apply phase (after the vm instance is created but not yet provisioned) with the following error:

Error: 2 problems:

- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.
- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.

Terraform Version

Terraform v0.12.6

  • provider.chef v0.2.0

Affected Resource(s)

azurerm_virtual_machine

Terraform Configuration Files

# Provisioner block from the azurerm_virtual_machine
# A list of server variables (name, size, etc.) are provided as a map to the "for_each" attribute
# Creating VM resources without the provisioner block works correctly
provisioner "chef" {
    node_name = each.key
    server_url = "https://chef.XXX.io/organizations/XXX/"
    user_key = var.chef_provisioner_private_key
    user_name = var.chef_admin_username
    channel = "stable"
    version = "15.1.36"
    client_options = ["chef_license 'accept'"]
    attributes_json = jsonencode({"cache_db_name" = join("_", ["pgcache", each.value.region, each.value.tenant])})
    recreate_client = true
    secret_key = var.chef_provisioner_databag_key
    ssl_verify_mode = ":verify_none"
    connection {
      host = azurerm_public_ip.pgsqlcache-ip[each.key].fqdn
      private_key = var.ssh_admin_private_key
      type = "ssh"
      user = var.ssh_admin_username
    }
    run_list = ["postgresql"]
  }

Expected Behavior

1 or more VM's should be created and provisioning applied to all of them using the "each" variable to provide individual VM details.

Actual Behavior

The "apply" is run and VM instances created but no provisioning occurs. The apply run ends with:

Error: 2 problems:

- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.
- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.

Steps to Reproduce

  1. terraform apply

References

hashicorp/terraform#17179

@pselle
Copy link

pselle commented Aug 14, 2019

Hi @kszlachcikowski! Thanks for the report! There are known issues with for_each in provisioners (hashicorp/terraform#22289) and this fix will go out in 0.12.7, which hopefully changes your issue's status :)

@pselle pselle added the bug label Aug 14, 2019
@pselle pselle self-assigned this Aug 14, 2019
@pselle
Copy link

pselle commented Aug 22, 2019

Terraform 0.12.7 is now released! https://github.com/hashicorp/terraform/blob/v0.12.7/CHANGELOG.md

@pselle pselle removed their assignment Aug 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants