Skip to content

Playbooks

Adhoc commands can run a single, simple task against a set of targeted hosts as a one-time command.

The real power of Ansible, however, is in learning how to use playbooks to run multiple, complex tasks against a set of targeted hosts in an easily repeatable manner.

A playbook is a text file written in YAML format, and is normally saved with the extension yml or yaml.

  • Only the space character can be used for indentation; tab characters are not allowed.

Example playbook:

---
- name: Configure user 
  hosts: server.example.com
  tasks:
    - name: joe exists with UID 1024
      user:
        name: joe
        uid: 1024
        state: present

Running Playbooks

The ansible-playbook command is used to run playbooks.

Increasing Output Verbosity

The ansible-playbook -v command provides additional information, with up to four total levels.

Syntax Verification

ansible-playbook --syntax-check example.yml

Executing a Dry Run

You can use the -C option to perform a dry run of the playbook execution. This causes Ansible to report what changes would have occurred if the playbook were executed, but does not make any actual changes to managed hosts.

ansible-playbook -C example.yml

Writing Multiple Plays

Example:

---
# This is a simple playbook with two plays
- name: first play
  hosts: web.example.com
  tasks:
    - name: first task
      yum:
        name: httpd
        status: present
    - name: second task
      service:
        name: httpd
        enabled: true

- name: second play
  hosts: database.example.com
  tasks:
    - name: first task
      service:
        name: mariadb
        enabled: true