hashicorp/nomad · error

distinct_hosts should be set to true or false; %v

Error message

distinct_hosts should be set to true or false; %v

What it means

Inside parseConstraints, a `distinct_hosts` constraint value is converted with parseBool. If that conversion fails (value is neither a bool nor a bool-parseable string), the error is wrapped as `distinct_hosts should be set to true or false; %v`.

Source

Thrown at command/volume_create_host.go:354

		// If "regexp" is provided, set the operand
		// to "regexp" and the value to the "RTarget"
		if constraint, ok := m[api.ConstraintRegex]; ok {
			m["Operand"] = api.ConstraintRegex
			m["RTarget"] = constraint
		}

		// If "set_contains" is provided, set the operand
		// to "set_contains" and the value to the "RTarget"
		if constraint, ok := m[api.ConstraintSetContains]; ok {
			m["Operand"] = api.ConstraintSetContains
			m["RTarget"] = constraint
		}

		if value, ok := m[api.ConstraintDistinctHosts]; ok {
			enabled, err := parseBool(value)
			if err != nil {
				return fmt.Errorf("distinct_hosts should be set to true or false; %v", err)
			}

			// If it is not enabled, skip the constraint.
			if !enabled {
				continue
			}

			m["Operand"] = api.ConstraintDistinctHosts
			m["RTarget"] = strconv.FormatBool(enabled)
		}

		if property, ok := m[api.ConstraintDistinctProperty]; ok {
			m["Operand"] = api.ConstraintDistinctProperty
			m["LTarget"] = property
		}

		// Build the constraint
		var c api.Constraint

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Set `distinct_hosts = true` or `distinct_hosts = false` as a bare boolean
  2. If it must be a string, use only values strconv.ParseBool accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False
  3. Remove the distinct_hosts key if you don't want the constraint
  4. Remember `false` simply skips the constraint, so deleting it is equivalent

Example fix

// before
constraint {
  distinct_hosts = "yes"
}

// after
constraint {
  distinct_hosts = true
}
Defensive patterns

Strategy: validation

Validate before calling

func validDistinctHosts(v interface{}) error {
	switch t := v.(type) {
	case bool:
		return nil
	case string:
		if _, err := strconv.ParseBool(t); err != nil {
			return fmt.Errorf("distinct_hosts %q must be true or false", t)
		}
		return nil
	default:
		return fmt.Errorf("distinct_hosts must be bool or bool-string, got %T", v)
	}
}

Type guard

func isBoolLike(v interface{}) bool {
	switch t := v.(type) {
	case bool:
		return true
	case string:
		_, err := strconv.ParseBool(t)
		return err == nil
	}
	return false
}

Try / catch

if err := parseBool(value); err != nil {
	return fmt.Errorf("set distinct_hosts to a bare true/false literal: %v", err)
}

Prevention

When it happens

Trigger: `nomad volume create` (host) where a constraint block contains `distinct_hosts` set to a non-boolean value such as "yes", 1, or an arbitrary string; note parseBool only accepts strconv.ParseBool-compatible strings (1/t/T/TRUE/true/True/0/f/F/FALSE/false/False) or actual bools.

Common situations: Using YAML-style truthiness ("yes"/"on"), JSON numbers (0/1), or quoted booleans with different capitalization like "True" with quotes is fine but "Yes" fails.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/7f2c7ae291da3b78. Report an issue: GitHub.