hashicorp/nomad · error

Affinity weight cannot be zero

Error message

Affinity weight cannot be zero

What it means

Affinity.Validate() enforces that the affinity weight is non-zero and within [-100, 100]. A weight of exactly 0 means the affinity has no effect, so Nomad treats it as a configuration mistake and rejects the job with 'Affinity weight cannot be zero'.

Source

Thrown at nomad/structs/structs.go:10233

		if _, err := semver.NewConstraint(a.RTarget); err != nil {
			mErr.Errors = append(mErr.Errors, fmt.Errorf("Semver affinity is invalid: %v", err))
		}
	case "=", "==", "is", "!=", "not", "<", "<=", ">", ">=":
		if a.RTarget == "" {
			mErr.Errors = append(mErr.Errors, fmt.Errorf("Operator %q requires an RTarget", a.Operand))
		}
	default:
		mErr.Errors = append(mErr.Errors, fmt.Errorf("Unknown affinity operator %q", a.Operand))
	}

	// Ensure we have an LTarget
	if a.LTarget == "" {
		mErr.Errors = append(mErr.Errors, fmt.Errorf("No LTarget provided but is required"))
	}

	// Ensure that weight is between -100 and 100, and not zero
	if a.Weight == 0 {
		mErr.Errors = append(mErr.Errors, fmt.Errorf("Affinity weight cannot be zero"))
	}

	if a.Weight > 100 || a.Weight < -100 {
		mErr.Errors = append(mErr.Errors, fmt.Errorf("Affinity weight must be within the range [-100,100]"))
	}

	return mErr.ErrorOrNil()
}

// DiffID fulfills the DiffableWithID interface.
func (a *Affinity) DiffID() string {
	return a.String()
}

// Spread is used to specify desired distribution of allocations according to weight
type Spread struct {
	// Attribute is the node attribute used as the spread criteria
	Attribute string

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Remove the weight field (defaults to 50) or set a non-zero value like weight = 50.
  2. If the affinity should be disabled, delete the affinity block instead of zeroing its weight.
  3. Use a negative weight (e.g. -50) if you want an anti-affinity, not zero.

Example fix

// before
affinity {
  attribute = "${node.datacenter}"
  value     = "dc1"
  weight    = 0
}
// after
affinity {
  attribute = "${node.datacenter}"
  value     = "dc1"
  weight    = 50
}
Defensive patterns

Strategy: validation

Validate before calling

if a.Weight == 0 { return errors.New("affinity weight must be non-zero; remove the block to disable") }

Prevention

When it happens

Trigger: Registering a job whose affinity has weight = 0, either explicitly in the HCL affinity block or by omitting... no — omission defaults to 50; this fires only when weight is explicitly set to 0 in HCL or the /v1/jobs payload.

Common situations: Setting weight = 0 to 'disable' an affinity (instead of deleting the block), template math computing 0, or API clients defaulting weight to 0 instead of leaving it unset.

Related errors


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