Skip to main content
Ansible playbooks are the cornerstone of automation with Ansible, providing a powerful language to orchestrate tasks across multiple servers and complex infrastructure setups. Within a playbook, you define a sequence of tasks that can range from executing simple commands on servers to deploying and configuring hundreds of virtual machines across public and private clouds.

Understanding Playbook Tasks

Below are examples of tasks that might be included in both simple and complex playbooks:
This diagram visually compares a simple playbook to a more complex one, illustrating tasks from basic server command execution to large-scale virtual machine deployments.
The image shows a comparison between simple and complex Ansible playbooks, detailing tasks for server commands and virtual machine deployments.
Playbooks are written in YAML format, so having a good grasp of YAML syntax is essential for writing error-free configurations.

Anatomy of an Ansible Playbook

An Ansible playbook is a YAML file that contains a list of plays. Each play targets specific hosts defined in your inventory and comprises multiple tasks. A task represents a single action, such as executing a command, running a script, installing a package, or restarting a service. Consider the following sample playbook:
In this example, tasks execute sequentially on the specified host (localhost). The playbook prints the current date, runs a server-side script, installs the HTTP service using the yum module, and finally starts the web server.

Multiple Plays in a Single Playbook

To further illustrate the structure, here’s an example of a playbook with two separate plays:
Each play is represented as a dictionary with keys like “name”, “hosts”, and “tasks”. It is crucial to maintain the specified order of tasks within a play since they are executed sequentially. Changing the order may result in unexpected behavior, for example, trying to start a service before it is installed.

Key Concepts Explained

  • Hosts Parameter: Defines the target for the play. Although the examples here use “localhost”, you can specify any host or group from your inventory. When a group is used, tasks are executed concurrently on all hosts within that group.
  • Modules: The core building blocks of Ansible. Some common modules demonstrated above include:
    • command
    • script
    • yum
    • service
There are hundreds of modules available by default. For detailed documentation, refer to the official Ansible documentation or use the ansible-doc -l command to list them.

Running Your Playbook

Once your playbook is set up, you can execute it using the following command:
For additional command options, run:
This command-line utility provides guidance on available parameters to further customize playbook execution.
The image explains a playbook as a YAML file defining activities (tasks) for hosts, including executing commands, running scripts, installing packages, and shutdown/restart actions.

Complete Sample Playbook

For quick reference, here’s the complete sample playbook:

Example Inventory Configuration

Below is an example inventory file that categorizes various hosts into groups:

Final Thoughts

Modules, tasks, and play definitions work in unison to create an orderly and efficient automation workflow. As you grow more comfortable with Ansible, you’ll discover advanced modules and strategies to manage configurations across a diverse range of environments. Happy automating!

Watch Video