8. Tasks

Before we can start with Tasks, we need to know YAML.

8.1. YAML is your friend

  • yaml stores structured data as text
  • Ansible uses YAML because it’s easy to read and write
  • nearly every YAML file starts with a list
  • an item in the list is a list of key / value pairs
  • the key / value pairs are commonly called hash or dictionary
  • a YAML file begins with ---
  • YAML cares about whitespaces and refuses tab character
  • the amount of whitespaces is free, often are 2 whitespaces used

8.1.1. String / Numeric value

  • string values don’t need quotes, but recommended (to avoid spaces)
  • simple key/value pairs
username: 'foo'
gid: 55

8.1.2. List

  • ordered sequence
  • can be nested
  • all members of a list begins with - (dash and space)
# a list named userlist with two items
userlist:
  - foo
  - bar

8.1.3. Dictionary

  • a dictionary is represented in a key: value form (space is needed)
  • unordered group of key/value pairs
  • indentation is needed
  • can be nested
# a dictionary named user with two key/values (string)
user:
  username: foo
  home: /home/foo

8.1.4. Nested

  • more complicated data structures are possible

  • dictionaries and lists can be nested

  • indentation is used for nesting

  • example
    • a list with 2 items
    • dictionaries whose values are lists
    • a mix of both
---
- pascal:                        <- 1st list item with a dictionary item
    name: Pascal Stauffer        <- key / value pair
    series:                      <- list with 3 items (indentation!)
      - suits
      - house of cards
      - dexter
- domi:                          <- 2nd list item with a dictionary item
    name: Domi Barton            <- key / value pair
    series:                      <- list with 3 items (indentation!)
      - breaking bad
      - happy endings
      - last man on earth

# dict.name / dict['name'] = string
# dict.series / dict['series'] = list

Hint

A list always containts a dash -, so you can differate between a list and a dictionary.

  • Dictionaries and lists can also be represented in a short form
# dictionary short form
martin: {name: Martin D'vloper, job: Developer, skill: Elite}

# list short form
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']

Hint

Don’t use the short form. It can be difficult to read and is hard to compare with a SCM tool.

8.1.5. Multiple lines

  • values can span multiple lines using | or >
  • | will include the newlines
  • > will ignore newlines
include_newlines: |
            exactly as you see
            will appear these three
            lines of poetry

ignore_newlines: >
            this is really a
            single line of text
            despite appearances

8.2. What’s a Task

  • a task defines a state
  • tasks are written in YAML syntax
  • calls a module with parameters
  • tasks are executed in order
  • tasks can be written on one or more lines
  • tasks run parallel on servers (default 5)
  • order of the hosts depends on the response time
  • the next task will started, if it run on every server
  • if a task fails, the following tasks will be skipped on this server
# recommended form
- name: install httpd package    <- documentation
  yum:                           <- module
    name: httpd                  <- arguments
    state: present

# short form
- apt: name=apache2 state=present

Hint

Don’t use the short form. With a lots of arguments, the line will be too long. And it’s difficutlt to compare with a SCM tool.

8.2.1. Run a ad-hoc task/command

  • a task can be run with the ansible binary.
ansible -m apt -a "name=apache2 state=latest" web1.pascal.lab