Saturday, April 18, 2026

Yaml_Ansible_3_list

 List (Array) # Lists start with - (dash + space), Indentation matters

A sequence of items, represented as a list (array) in YAML.
Lists can be defined using either block style (with dashes) or inline style (with square brackets).
List Multiple-items

Simple List (Basic)
servers:
- web1
- web2
- db1

Inline List (Flow Style)
servers: [web1, web2, db1]

Nested List
matrix:
- - 1
- 2
- - 3
- 4



List of Maps (Very Important in Ansible 🚀)
users:
- name: John
age: 25
- name: Alice
age: 30

🔥 How to "call" (access) list of maps
Access full list {{ users }}
Access first item {{ users[0] }}
Access specific value {{ users[0].name }} # 👉 Output: John
Loop through list (Most important 🚀)
- name: Print users
debug:
msg: "{{ item.name }} is {{ item.age }} years old"
loop: "{{ users }}"


List inside Map
employee:
name: John
skills:
- Python
- Ansible
- Docker
👉 Here:
employee → map
skills → list inside the map
Access full list {{ employee.skills }}
Access specific item (index) {{ employee.skills[0] }} #👉 Output: Python
Loop through the list
- name: Print skills
debug:
msg: "{{ item }}"
loop: "{{ employee.skills }}"

Use inside message
- name: Print employee info
debug:
msg: "{{ employee.name }} knows {{ employee.skills[1] }}"
👉 Output: John knows Ansible

Structure How to Access
Map → key map.key
List → index list[0]
Map → List map.list_key[0]

No comments:

Post a Comment