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] }}
No comments:
Post a Comment