BacklogZero®

Ansible in a Minute · Ansible Core ·

Use a dictionary variable for key-value data

Short answer

Define a YAML dictionary and read one value with bracket notation. Bracket notation always works.

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 user dictionary with name and role and print the name

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 a user as a dictionary variable
  hosts: all
  gather_facts: false
  vars:
    user:
      name: leeroy
      role: astronaut
  tasks:
    - name: Print the user name
      ansible.builtin.debug:
        msg: "{{ user['name'] }}"

How it works

A dictionary maps keys to values. user['name'] is leeroy. user.role is the same as user['role']. Prefer brackets when a key might collide with a Python dictionary method.

When to use it

  • Storing related fields such as name and role
  • Passing a structured profile into later tasks

Requirements

  • Ansible: >=2.14

Validation

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

Notes

Use a list when the values are peers. Use a dictionary when each value has a name. user['name'] and user.name both return leeroy. Use brackets for keys such as items, keys, or values.

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 dictionary variable for key-value data · Level Up Labs