Map Structure
Map refers to Dictionary or Key-Value pair structure,
Map Structured-data
A YAML map (mapping) is basically a key → value structure (like a dictionary in Python or JSON object).
name: Alagarasan
role: DevOps Consultant
experience: 8
👉 Here:
name, role, experience → keys
Values → strings / numbers
- name: Create VM
hosts: localhost
vars:
vm_name: test-vm
vm_cpu: 2
👉 Here:
vars is a map
Inside it → vm_name, vm_cpu are keys
Types of Maps
Inline Map
Block Map
Inline Map
person: {name: Alagarasan, role: DevOps Consultant, experience: 8}
Block Map
person:
name: Alagarasan
role: DevOps Consultant
experience: 8
Access full map
- debug:
var: person
Access individual values
- debug:
msg: "{{ person.name }}"
- debug:
msg: "{{ person.role }}"
🔹 Nested Map (Map inside Map)
Inside map we always have key value pairs. We can have multiple key value pairs inside a map. We can also have a map inside a map which is called nested map.
employee:
name: Alagarasan
role: DevOps Consultant
skills:
primary: Ansible
secondary: AWS
Access nested value
- debug:
msg: "{{ person.address.city }}"
👉 Structure:
employee → parent key
Inside it → another map
Map with list
person:
name: Alagarasan
skills:
- Ansible
- Docker
- Kubernetes
Nested Map + List (Real-world 🔥🔥)
servers:
- name: web01
config:
ip: 192.168.1.10
ports:
- 80
- 443
- name: web02
config:
ip: 192.168.1.11
ports:
- 8080
👉 Access:
{{ servers[0].config.ip }}
{{ servers[0].config.ports[1] }}
Quoting in Ansible Strings
A variable right after specifying the module, the YAML parser will misinterpret the variable reference as the beginning of an inline dictionary.
- name: Perform some task
command: {{ myapp }} -a foo
Ansible will try to parse the first part of {{ myapp }} -a foo as a dictionary instead of a string, and will return an error. In this case, you must quote the arguments:
- name: Perform some task
command: "{{ myapp }} -a foo"
A similar problem arises if your argument contains a colon. For example:
- name: Show a debug message
debug:
msg: The debug module will print a message: neat, eh?
The colon in the msg argument trips up the YAML parser.
To get around this, you need to quote the entire msg string. Single and double quotes are both correct; Bas prefers to use double quotes when the string has variables:
- name: Show a debug message
debug:
msg: "The debug module will print a message: neat, eh?"
This will make the YAML parser happy. Ansible supports alternating single and double quotes,
'single quotes' → literal strings
"double quotes" → interpreted strings (supports escaping + special chars)
Double Quotes
- name: Print variable
debug:
msg: "Hello {{ user }}"
👉 {{ user }} will be evaluated
msg: "Line1\nLine2"
👉 Produces:
Line1
Line2
Single Quotes
- name: Print variable
debug:
msg: 'Hello {{ user }}'
👉 Jinja2 still processes it, BUT:
Escaping behaves differently
Harder to nest quotes
msg: 'Line1\nLine2'
👉 Produces:
Line1\nLine2
Loop
A loop executes the task multiple times, each time replacing item with different values from the specified list:
- name: Install multiple packages
hosts: all
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop:
- nginx
- git
- curl
Loop with List Variable
vars:
packages:
- nginx
- git
- curl
tasks:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
Loop with Dictionary (Key-Value)
vars:
users:
user1: dev
user2: admin
tasks:
- name: Create users
user:
name: "{{ item.key }}"
comment: "{{ item.value }}"
loop: "{{ users | dict2items }}"
👉 dict2items converts dictionary → list