Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail

YAML is a file format similar in intent to JSON, but generally easier for humans to read and write.

Start of File

YAML files are supposed to start with three dashes to indicate the beginning of the document:
– – –
However, if you forget to put those three dashes at the top of your playbook files, Ansible won’t complain.



Comments start with a number sign and apply to the end of the line, the same as in shell scripts, Python, and Ruby:
# This is a YAML comment


In general, YAML strings don’t have to be quoted even though they have spaces in them, and you can quote them if you prefer.



YAML truthy
true, True, TRUE, yes, Yes, YES, on, On, ON, y, Y
YAML falsey
false, False, FALSE, no, No, NO, off, Off, OFF, n, N


LIsts (sequences)

 delimited with hyphens:

- My Fair Lady
- Oklahoma
- The Pirates of Penzance

Similar to dictionaries in Python:

address: 742 Evergreen Terrace
city: Springfield
state: North Takoma
Line Folding


Line folding break up one line across multiple lines, and Ansible treat the string as if it were a single line.
You can do this with YAML using line folding with the greater than (
>) character.

address: >
Department of Computer Science,
A.V. Williams Building,
University of Maryland
city: College Park
state: Maryland
Playbook in Ansible

A play book is a list of dictionaries , and dictionary is called play in the Ansible playbook.

Let’s take a playbook as example:

- name: Configure webserver with nginx
  hosts: webservers
  sudo: True
    - name: install nginx
      apt: name=nginx update_cache=yes
    - name: copy nginx config file
      copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
    - name: enable configuration
      file: >
    - name: copy index.html
      template: src=templates/index.html.j2
               dest=/usr/share/nginx/html/index.html mode=0644
    - name: restart nginx
      service: name=nginx state=restarted 

Every play must contain:
• A set of
hosts to configure
• A list of
tasks to be executed on those hosts


In the example playbook, we have 5 tasks.

The first task of the play:

- name: install nginx
apt: name=nginx update_cache=yes

The name dictionary is optional, so it’s perfectly valid to write the first task like: - apt: name=nginx update_cache=yes
Every task must contain a key with the name of a module and a value with the arguments to that module. In the preceding example, the module name is apt and the arguments are name=nginx update_cache=yes.
These arguments tell the apt module to install the package named nginx and to update the package cache (the equivalent of doing an apt-get update) before installing the package.

Optional settings

  A comment that describes what the play is about. Ansible will print this out when the play starts to run.
  If true, Ansible will run every task by sudo’ing as (by default) the root user. This is useful when managing Ubuntu servers, since by default you cannot SSH as the root user.
  A list of variables and values. We’ll see this in action later in this chapter.

Running state

When you run ansible-playbook, Ansible outputs status information for each task it executes in the play.
OKIf the state of the host matches the arguments of the module, then Ansible takes no action on the host and responds with a state of ok.
Changed – On the other hand, if there is a difference between the state of the host and the arguments to the module, then Ansible will change the state of the host and return