HashiConf Our community conference is taking place in San Francisco and online October 10-12. Register now
  • Infrastructure
    • terraform
    • packer
  • Networking
    • consul
  • Security
    • vault
    • boundary
  • Applications
    • nomad
    • waypoint
    • vagrant
  • HashiCorp Cloud Platform

    A fully managed platform to automate infrastructure on any cloud with HashiCorp products.

    • consul
    • terraform
    • vault
    • packerbeta
    Visit cloud.hashicorp.com
Sentinel
  • Intro
  • Docs
Download
    • v0.21.x (latest)
    • v0.20.x
    • v0.19.x
    • v0.18.x
    • v0.17.x
    • v0.16.x
    • v0.15.x
    • v0.14.x
    • v0.13.x
  • Release Notes
    • Overview
    • Policy as Code
    • Policy Language
    • Imports
    • Enforcement Levels
    • Overview
    • Override Files
    • Remote Sources
    • Overview
    • apply
    • fmt
    • test
    • Overview
    • Basics
    • Rules
    • Traces
    • Testing
    • Imports
    • Debugging
    • Overview
    • Modules
    • Plugins
    • Static Imports
    • Internals

    • Overview
    • Variables
    • Values
    • Lists
    • Maps
    • Rules
    • Imports
    • Parameters
    • Boolean Expressions
    • Arithmetic
    • Slices
    • Conditionals
    • Loops
    • Collection Operations
    • Functions
    • Scope
    • Undefined
    • Logging and Errors
    • Specification
    • Overview
    • append
    • delete
    • error
    • keys
    • length
    • print
    • range
    • values
    • Overview
    • base64
    • decimal
    • http
    • json
    • runtime
    • sockaddr
    • strings
    • time
    • types
    • units
    • version

  • Consul
  • Nomad
  • Terraform
  • Vault
Type '/' to Search

»Language: Loops

Loop statements allow you to execute a body of code for each element in a collection or for some fixed number of times.

Loop statements may only appear outside of rule expressions, such as in functions or in the global scope of a policy. This is because rules are only allowed to contain a single boolean expression.

»For Statements

for statements allow repeated execution of a block for each element in a collection.

Example:

// A basic sum
count = 0
for [1, 2, 3] as num {
    count += num
}

The syntax is for COLLECTION as value. This will iterate over the collection, assigning each element to value. In the example above, each element is assigned to num. The body is executed for each element. In the example above, the body adds num to the count variable. This creates a basic sum of all values in the collection.

For a map, the assigned element is the key in the map. In the example below, name would be assigned map keys.

list = []
for { "a": 1, "b": 2 } as name {
    append(list, name)
}

print(list) // ["a" "b"]

An alternate syntax is for COLLECTION as key, value. This will assign both the key and value to a variable. For a list, the key is the element index. For a map, it is the key and value is assigned the element value. Example:

count = 0
for { "a": 1, "b": 2 } as name, num {
    count += num
}

print(count) // 3

»Scoping

The body of a for statement creates a new scope. If a variable is assigned within the body of a for statement that isn't assigned in a parent scope, that variable will only exist for the duration of the body execution.

Example:

for list as value {
    a = 42
}

print(a) // undefined
a = 18
for list as value {
    a = 42
}

print(a) // 18
  • ProvisionMulti-Cloud Infrastructure
  • SecureMulti-Cloud Security
  • ConnectMulti-Cloud Networking
  • RunMulti-Cloud Orchestration
Products
  • Terraform
  • Vault
  • Consul
  • Nomad
  • Vagrant
  • Packer
  • Boundary NEW
  • Waypoint NEW
  • Sentinel
Resources
  • Blog
  • Tutorials
  • Community
  • Events
  • Integrations
  • Library
  • Partners
  • Podcast
  • Support
  • Training
Company
  • About Us
  • JobsWe're Hiring
  • Press Center
  • Brand
  • Contact Us
  • System Status
  • Cookie Manager
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
stdin: is not a tty