BacklogZero®

Ansible in a Minute · Ansible Core ·

Use a list variable for multiple values

Short answer

Define a YAML list variable and select an item by index. The first item is index 0.

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

define a region list and print the first item

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Define regions as a list variable
  hosts: all
  gather_facts: false
  vars:
    region:
      - northeast
      - southeast
      - midwest
  tasks:
    - name: Print the first region
      ansible.builtin.debug:
        msg: "{{ region[0] }}"

How it works

A list variable stores several values under one name. YAML lists use a dash per item. Index 0 is the first value, so region[0] is northeast.

When to use it

  • Storing several names, paths, or regions in one variable
  • Selecting one list item by index in a later task

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-22

Notes

You can also write the same list as region: [northeast, southeast, midwest]. Use loop: "{{ region }}" when you must run a task for every item.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today

Related guides

Back to Ansible in a Minute

Use a list variable for multiple values · Level Up Labs