Packer: Packer validate does not accept blank user variable

Created on 4 Feb 2015  路  5Comments  路  Source: hashicorp/packer

Using this JSON file

{
  "variables": {
    "do_api_token": ""
  },
  "builders": [
  {
    "type": "digitalocean",
    "api_token": "{{user `do_api_token`}}"
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sudo apt-get update",
      "sudo apt-get install -y redis-server"
    ]
  }]
}

results in this error

Template validation failed. Errors are shown below.

Errors validating build 'digitalocean'. 2 error(s) occurred:

* a client_id for v1 auth or api_token for v2 auth must be specified
* a api_key for v1 auth or api_token for v2 auth must be specified

Shouldn't the validation pass since a user variable is used? I don't see the benefit of specifying a user variable during validation, for example

packer validate -var 'do_api_token=123' do2.json
bug buildedigitalocean

All 5 comments

Hi @hanxue

Thank you for opening an issue. The validation error you are getting is a real one from the digital ocean builder. The validation is coming from the builders block, not the variables block. The v1 digital ocean API requires you specify a client_id and api_key in the build block:

{
  "type":"digitalocean",
  "client_id":"...",
  "api_key":"..."
}

You are only specifying api_token, which is the v2 api. Looking at the code, it appears that the digital ocean builder should intelligently switch when the key is given.

To better diagnose this issue, could you please:

  1. Run packer version and paste the result
  2. Try using a different builder that does not have conditional switching logic. It would be very helpful in identifying if this is a bug in the digital ocean builder's switching logic or something more top-level in the actual validation as originally reported.

Hi @sethvargo

I came across with the same issue and I would like to help:

  1. Run packer version and paste the result
#packer version
Packer v0.7.5

Using JSON:

{
  "variables": {
    "do_api_token": ""
  },
  "builders": [{
    "type": "digitalocean",
    "api_token": "{{user `do_api_token`}}",
    "image": "ubuntu-14-04-x64",
    "region": "nyc3"
  }],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sleep 30",
      "sudo apt-get update",
      "sudo apt-get install -y htop"
    ]
  }]
}

When:

#packer validate example.json

I get this error:

Template validation failed. Errors are shown below.

Errors validating build 'digitalocean'. 2 error(s) occurred:

* a client_id for v1 auth or api_token for v2 auth must be specified
* a api_key for v1 auth or api_token for v2 auth must be specified
  1. Try using a different builder

Now using the amazon-ebs builder:

{
  "variables": {
    "aws_access_key": "", 
    "aws_secret_key": ""
  },  

  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-de0d9eb7",
    "instance_type": "t1.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-quick-start {{timestamp}}"
  }]  
}

I get:

#packer validate example_2.json
Template validated successfully.

It seems to be a bug in the digital ocean builder's switching logic.

Validate is not meant to simply validate that params have values, but also that those values are correct in some cases. For this reason, we require the full valid variable values for validation, or you can run syntax-only validation as well.

This issue still happens. Although the validation fails, the build runs as expected. I'm following the https://www.packer.io/intro/getting-started/parallel-builds.html tutorial.

Getting started docs for Parallel Builds are out of date.

Packer Version:

$ packer version
Packer v0.12.1

Template:

{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": "",
    "do_api_token": ""
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-east-1",
      "source_ami": "ami-fce3c696",
      "instance_type": "t2.micro",
      "ssh_username": "ubuntu",
      "ami_name": "bear packer-example {{timestamp}}"
    },
    {
      "type": "digitalocean",
      "api_token": "{{user `do_api_token`}}",
      "image": "ubuntu-14-04-x64",
      "region": "nyc3",
      "size": "512mb"
    }
  ],
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sleep 30",
      "sudo apt-get update",
      "sudo apt-get install -y redis-server"
    ]
  }]
}

Validate & Output:

$ packer validate example.json 
Template validation failed. Errors are shown below.

Errors validating build 'digitalocean'. 2 error(s) occurred:

* An ssh_username must be specified
  Note: some builders used to default ssh_username to "root".
* api_token for auth must be specified

Clearly missing ssh_username, when added it still complains about api_token which is set as a variable from the user. Seeing as AWS builder accepts this for validation I would think this is a problem with the DigitalOcean builder.

Calling the validate with variables, this will validate. However, the docs do not call this out.

$ packer validate -var 'do_api_token=FAKE' example.json 
Template validated successfully.
Was this page helpful?
0 / 5 - 0 ratings