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

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with .... This is part of the YAML format and indicates the start and end of a document.
However, if you forget to put those three dashes at the top of your playbook files, Ansible won’t complain.

Comments

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

Strings

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.

SPACES VS TABS

YAML uses spaces, period. Do not use tabs in your SLS files! If strange errors are coming up in rendering SLS files, make sure to check that no tabs have crept in! In Vim, after enabling search highlighting with: :set hlsearch, you can check with the following key sequence in normal mode(you can hit ESC twice to be sure): /, Ctrl-v, Tab, then hit Enter. Also, you can convert tabs to 2 spaces by these commands in Vim::set tabstop=2 expandtab and then :retab.

INDENTATION

The suggested syntax for YAML files is to use 2 spaces for indentation, but YAML will follow whatever indentation system that the individual file uses. Indentation of two spaces works very well for SLS files given the fact that the data is uniform and not deeply nested.

/etc/http/conf/http.conf:
  file:
    - managed
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
         custom_var: "override"
    - defaults:
         custom_var: "default value"
         other_var: 123

Notice that while the indentation is two spaces per level, for the values under the context and defaults options there is a four-space indent. If only two spaces are used to indent, then those keys will be considered part of the same dictionary that contains the context key, and so the data will not be loaded correctly. If using a double indent is not desirable, then a deeply-nested dict can be declared with curly braces:

/etc/http/conf/http.conf:
  file:
    - managed
    - source: salt://apache/http.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context: {
      custom_var: "override" }
    - defaults: {
      custom_var: "default value",
      other_var: 123 }
Booleans

There are multiple ways to express booleans:

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)

 All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):

- My Fair Lady
- Oklahoma
- The Pirates of Penzance
Dictionaries

Similar to dictionaries in Python, a dictionary is represented in a simple key: value form (the colon must be followed by a space, All keys are case-sensitive):

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
  tasks:
    - 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: >
        dest=/etc/nginx/sites-enabled/default
        src=/etc/nginx/sites-available/default
        state=link
    - 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

Tasks

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

name
  A comment that describes what the play is about. Ansible will print this out when the play starts to run.
sudo
  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.
vars
  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
changed.

Reference

https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

 

Issue:

 

| FAILED | rc=-1 >>

to use the 'ssh' connection type with passwords, you must install the sshpass program

Ansible says it needs sshpass to be able to run ssh commands without prompting passwords.

Ok, let’s do the last step by installing sshpass;

On Ubuntu

apt-get install sshpass

or if you’re running Ansible on OSX

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

 

 

Reference

YAML tutorial:  https://gettaurus.org/docs/YAMLTutorial/

YAML indentation: https://docs.saltstack.com/en/latest/topics/troubleshooting/yaml_idiosyncrasies.html