Ansible Playbooks
Ansible Playbooks are the core mechanism for automating tasks using Ansible. Playbooks allow you to define a set of instructions, such as configuring servers, deploying applications, or orchestrating complex workflows, in a simple YAML format.
1. What is an Ansible Playbook?
Ansible Playbooks are structured YAML files that define a set of tasks to execute on remote systems. They describe the desired state of a system, allowing Ansible to bring the target system to that state by performing the tasks defined in the playbook. Each playbook contains one or more "plays," which map hosts to roles or tasks.
Key Concepts:
- Plays: A set of tasks to be applied to hosts.
- Tasks: Individual actions, such as installing a package or creating a file.
- Modules: Units of work executed by tasks (e.g.,
ansible.builtin.apt,ansible.builtin.copy). - Roles: A method to group tasks and organize playbooks into reusable components.
2. Basic Structure of a Playbook
A minimal playbook consists of:
---
- name: Example Playbook
hosts: all
become: true
tasks:
- name: Install Nginx
ansible.builtin.apt:
name: nginx
state: present
Explanation:
name: Describes the playbook or task.hosts: Defines the group of systems where the tasks will be executed.become: Ensures privilege escalation (running tasks asroot).tasks: A list of tasks to be performed (e.g., installingnginxin this case).
3. Common Task Operations
Tasks define actions to be performed on a host, using modules like package managers, file operations, or service control. Each task is self-contained and runs independently.
Examples of common tasks:
-
Install a package:
- name: Install a package
ansible.builtin.apt:
name: python3
state: present -
Copy a file:
- name: Copy a file to remote
ansible.builtin.copy:
src: /local/path/file.txt
dest: /remote/path/file.txt -
Start a service:
- name: Start Nginx service
ansible.builtin.service:
name: nginx
state: started
4. Variables in Playbooks
Variables allow you to make your playbooks more flexible by avoiding hardcoding values. You can define variables directly in playbooks or load them from external files.
Example with variables:
---
- name: Install web server
hosts: webservers
vars:
pkg_name: nginx
tasks:
- name: Install web server package
ansible.builtin.apt:
name: "{{ pkg_name }}"
state: present
Explanation: The variable pkg_name is defined as nginx, and is used within the task by referencing {{ pkg_name }}.
5. Conditionals in Playbooks
You can control whether a task runs based on conditions using the when clause.
Example:
- name: Install package only on Ubuntu
ansible.builtin.apt:
name: nginx
state: present
when: ansible_facts['os_family'] == 'Debian'
Explanation: This task will only run if the host's operating system family is Debian-based (e.g., Ubuntu).
6. Loops in Playbooks
Ansible allows looping over a list of items, which is useful when performing similar actions on multiple items.
Example:
- name: Install multiple packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- nginx
- curl
- git
7. Roles in Playbooks
Roles help in organizing tasks and variables into reusable components. Roles are typically stored in a roles/ directory, and each role can contain tasks, handlers, variables, and more.
Example of using a role in a playbook:
---
- name: Configure a web server
hosts: webservers
roles:
- nginx
Explanation: This playbook calls the nginx role, which might handle the entire configuration and setup of an Nginx web server.
8. Handlers
Handlers are similar to tasks but are only triggered when notified by other tasks. They are typically used for restarting services after configuration changes.
Example:
tasks:
- name: Install Nginx
ansible.builtin.apt:
name: nginx
state: present
notify:
- Restart Nginx
handlers:
- name: Restart Nginx
ansible.builtin.service:
name: nginx
state: restarted
Explanation: The handler Restart Nginx will only run if the Install Nginx task changes the system state (i.e., installs the package).
9. Example Full Playbook
---
- name: Set up web server
hosts: webservers
become: true
vars:
pkg_name: nginx
document_root: /var/www/html
tasks:
- name: Install Nginx
ansible.builtin.apt:
name: "{{ pkg_name }}"
state: present
- name: Copy index.html to document root
ansible.builtin.copy:
src: ./index.html
dest: "{{ document_root }}/index.html"
- name: Start Nginx service
ansible.builtin.service:
name: nginx
state: started
handlers:
- name: Restart Nginx
ansible.builtin.service:
name: nginx
state: restarted
Explanation:
- Installs Nginx.
- Copies a file (
index.html) to the web server's root directory. - Ensures the Nginx service is started.
- If Nginx is installed or reconfigured, it will restart the service via a handler.
10. Running a Playbook
Once you have written your playbook, you can execute it using the ansible-playbook command:
ansible-playbook playbook.yml
You can also specify an inventory file to target specific hosts:
ansible-playbook -i inventory playbook.yml
Conclusion
Ansible Playbooks are a powerful way to automate system configuration, application deployment, and other infrastructure tasks. With their simple YAML syntax and modular nature, playbooks make it easy to define and manage complex tasks in a clear and scalable way.