Understanding Ansible Playbook Structure
Introduction
Ansible playbooks are a way to send commands to remote systems through scripts. They are simple, yet powerful, and help automate configuration management and deployment tasks. This tutorial will guide you through the structure of an Ansible playbook and explain each component in detail.
Basic Structure of a Playbook
A playbook is a YAML file containing a series of plays. Each play specifies the tasks to be executed on a set of hosts. Here is a basic structure of a playbook:
--- - name: Playbook Example hosts: webservers become: yes tasks: - name: Ensure Apache is installed yum: name: httpd state: present - name: Start Apache service service: name: httpd state: started
Components of a Playbook
Let's break down the components of the playbook:
1. Playbook Header
The playbook starts with three dashes (---
) indicating the beginning of the YAML document.
---
2. Play
A play maps a set of hosts to roles, which in turn define the tasks to be executed on those hosts. Each play starts with a dash (-
) and includes the following fields:
- name: An optional description of the play.
- hosts: Specifies the target hosts.
- become: Optional privilege escalation (e.g., sudo).
- tasks: A list of tasks to be executed.
- name: Playbook Example hosts: webservers become: yes
3. Tasks
Each play contains a list of tasks. Tasks are executed in the order they are defined. Each task has a name and a module that performs a specific action. For example, installing a package or starting a service:
tasks: - name: Ensure Apache is installed yum: name: httpd state: present - name: Start Apache service service: name: httpd state: started
Variables
Variables allow you to store values that can be reused throughout the playbook. Variables can be defined in multiple ways, including:
- Directly in the playbook.
- In separate variable files.
- As facts gathered from the hosts.
Here's an example of using variables in a playbook:
--- - name: Playbook with Variables hosts: webservers vars: http_port: 80 max_clients: 200 tasks: - name: Ensure Apache is installed yum: name: httpd state: present - name: Configure Apache template: src: /path/to/httpd.conf.j2 dest: /etc/httpd/conf/httpd.conf
Handlers
Handlers are tasks that are only run when notified by other tasks. They are useful for actions that should only be taken when there is a change, such as restarting a service after a configuration file is modified:
--- - name: Playbook with Handlers hosts: webservers tasks: - name: Ensure Apache is installed yum: name: httpd state: present notify: - Restart Apache handlers: - name: Restart Apache service: name: httpd state: restarted
Including and Importing Files
To keep playbooks organized, you can include or import tasks, roles, and other playbooks. This helps in managing large and complex automation tasks:
--- - name: Main Playbook hosts: webservers tasks: - include: tasks/install.yml - include: tasks/configure.yml
Conclusion
This tutorial covered the basic structure of an Ansible playbook, including plays, tasks, variables, handlers, and how to include or import other files. Understanding these components will help you create effective and organized playbooks for automating your infrastructure tasks.