In this section, we will delve into the structure of an Ansible playbook. Understanding the structure is crucial for writing effective and maintainable playbooks. We will cover the following key components:
- YAML Basics
- Playbook Anatomy
- Tasks
- Handlers
- Variables
- Includes and Imports
- YAML Basics
Ansible playbooks are written in YAML (YAML Ain't Markup Language). Here are some basic rules of YAML:
- YAML files use indentation to indicate structure. Indentation is done using spaces, not tabs.
- Lists are indicated by a dash (
-). - Key-value pairs are separated by a colon (
:).
Example:
# A simple YAML example
- name: Install Apache
hosts: webservers
tasks:
- name: Ensure Apache is installed
yum:
name: httpd
state: present
- Playbook Anatomy
A playbook is a collection of plays. Each play targets a group of hosts and defines tasks to be executed on those hosts.
Basic Structure:
- name: Playbook Name
hosts: target_hosts
become: yes/no
vars:
variable_name: value
tasks:
- name: Task Name
module_name:
parameter1: value1
parameter2: value2
handlers:
- name: Handler Name
module_name:
parameter1: value1Explanation:
- name: A description of the playbook or play.
- hosts: The target hosts on which the playbook will run.
- become: Whether to escalate privileges (e.g., using
sudo). - vars: Variables used within the playbook.
- tasks: A list of tasks to be executed.
- handlers: Special tasks that are triggered by other tasks.
- Tasks
Tasks are the core of a playbook. Each task uses an Ansible module to perform an action.
Example:
Explanation:
- name: A description of the task.
- yum: The module used (in this case, the
yummodule for package management). - name: The package to be managed.
- state: The desired state of the package (
presentorabsent).
- Handlers
Handlers are tasks that are triggered by other tasks using the notify directive. They are typically used for actions like restarting services.
Example:
tasks:
- name: Ensure Apache is installed
yum:
name: httpd
state: present
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: httpd
state: restartedExplanation:
- notify: Specifies the handler to be triggered.
- handlers: Defines the handler tasks.
- Variables
Variables allow you to store values that can be reused throughout the playbook.
Example:
vars:
http_port: 80
tasks:
- name: Ensure Apache is listening on port 80
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: "Listen {{ http_port }}"Explanation:
- vars: Defines variables.
- {{ variable_name }}: Syntax to use the variable.
- Includes and Imports
Includes and imports allow you to break down your playbook into smaller, reusable files.
Example:
Explanation:
- import_tasks: Imports tasks from another file.
Practical Exercise
Task:
Create a playbook that installs Nginx on a group of web servers, ensures the service is running, and sets up a basic HTML file.
Solution:
- name: Install and configure Nginx
hosts: webservers
become: yes
vars:
nginx_package: nginx
nginx_service: nginx
index_file: /usr/share/nginx/html/index.html
index_content: "<html><body><h1>Welcome to Nginx!</h1></body></html>"
tasks:
- name: Install Nginx
yum:
name: "{{ nginx_package }}"
state: present
- name: Ensure Nginx is running
service:
name: "{{ nginx_service }}"
state: started
enabled: yes
- name: Create index.html
copy:
content: "{{ index_content }}"
dest: "{{ index_file }}"
handlers:
- name: Restart Nginx
service:
name: "{{ nginx_service }}"
state: restartedExplanation:
- vars: Defines variables for package name, service name, file path, and content.
- tasks: Installs Nginx, ensures it is running, and creates an index.html file.
- handlers: Defines a handler to restart Nginx if needed.
Conclusion
Understanding the structure of an Ansible playbook is fundamental to writing effective automation scripts. By mastering the components such as tasks, handlers, variables, and includes, you can create modular, reusable, and maintainable playbooks. In the next section, we will explore variables and facts in more detail, which will further enhance your playbook writing skills.
Ansible: From Beginner to Advanced
Module 1: Introduction to Ansible
Module 2: Ansible Basics
Module 3: Playbooks
- Introduction to Playbooks
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
Module 4: Roles
Module 5: Advanced Playbook Techniques
Module 6: Ansible Galaxy
Module 7: Ansible Tower
- Introduction to Ansible Tower
- Installing Ansible Tower
- Using Ansible Tower
- Managing Projects and Inventories
