Using Ansible Playbooks#

YAML#

Human and machine-readable format for playbooks.

  • Begin/end tags

  • Lists

  • Dictionaries

  • Indentation

  • Line folding

Begin/end tags#

All YAML files begin with three dashes “---” and end with three dots “...”.
---
# Example YAML document
- list item 1
- list item 2
...

Lists#

Ansible plays contain list of items e.g. tasks, hosts, options. Dictionaries can be one line or multiline with “>” or “|”, or as separate items.

---
- name: configure app hosts
  hosts app
  become: true
  tasks:
    - package: name=apache state=present
    - copy: >
      name=ntp.conf
      src=files/ntp.conf
      dest=/etc/ntp.conf
    - user: |
      name=jack
      state=present
    - user:
      name: john
      state: present
...

Dictionaries#

Indentation#

Line folding#

Anatomy of a Playbook#

Writing your first playbook#

Example playbook called systems.yml:

---
- name: configure app hosts
  hosts app
  become: true
  tasks:
    - package: name=apache state=present
    - copy: >
      name=ntp.conf
      src=files/ntp.conf
      dest=/etc/ntp.conf
    - user: |
      name=jack
      state=present
    - user:
      name: john
      state: present
...

Syntax check your playbook.

(venv) $ ansible-playbook systems.yml --system-check

YAML lint your playbook.

(venv) $ yamllint systems.yml

List Hosts, Tasks and Tags in a Playbook

(venv) $ ansible-playbook systems.yml --list-hosts
(venv) $ ansible-playbook systems.yml --list-tasks
(venv) $ ansible-playbook systems.yml --list-tags

Dry run

(venv) $ ansible-playbook systems.yml --check
(venv) $ ansible-playbook systems.yml -C

Apply all tasks from playbook

(venv) $ ansible-playbook systems.yml