Mapping and list
web:
hosts:
web01:
ansible_host: 192.0.2.21
web02:
ansible_host: 192.0.2.22- web and hosts are mapping keys.
- web01 and web02 are nested host keys.
- Indentation shows which values belong together.
Learn only the YAML structures needed to read and write clear Ansible playbooks.
You do not need to become a YAML expert before using Ansible. Most beginner playbooks use mappings (key-value pairs), lists and nested combinations of the two.
Indentation represents structure, so consistency matters. Use spaces rather than tabs and keep playbooks visually simple.
The leading dash before a play means 'this is an item in a list'. Inside the play, keys such as name, hosts, become and tasks form a mapping. tasks itself contains another list, where each item is a task.
Quote values containing special characters, template expressions or values that YAML might interpret unexpectedly. File modes are commonly quoted, for example '0644'. Clear quoting is preferable to relying on subtle parser behaviour.
web:
hosts:
web01:
ansible_host: 192.0.2.21
web02:
ansible_host: 192.0.2.22tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Start nginx
ansible.builtin.service:
name: nginx
state: startedGoal: Practice identifying lists, mappings and nesting before running a playbook.
- name: YAML practice
hosts: web
become: true
tasks:
- name: Ensure a directory exists
ansible.builtin.file:
path: /tmp/ansible-demo
state: directory
mode: '0755'ansible-playbook -i inventory.ini syntax.yml --syntax-checkMove one module argument to the wrong indentation level and observe the parser error. Restore the correct structure before continuing.
Readable YAML is operational safety: reviewers should be able to understand a change before it reaches production.
Open each answer only after you have tried to answer the question yourself.
An item in a list.
Indentation expresses parent-child structure in YAML.
ansible-playbook <playbook> --syntax-check.