Introduction
Ansible is a powerful IT automation tool that simplifies the management of complex infrastructures. One of the core components of Ansible is the Playbook a simple and powerful configuration management and multi-machine deployment system. In this blog we'll dive deep into understanding Ansible Playbooks, their structure and how to write and run a basic Playbook to install a package. By the end of this guide you'll be well-equipped to craft your first Ansible Playbook.
What is an Ansible Playbook?
Ansible Playbooks are YAML files that describe a series of tasks to be executed on a set of hosts. They are human-readable and can be versioned and shared, making them an excellent tool for automating repetitive tasks, ensuring consistency and managing complex deployments.
Playbooks are designed to be simple and reusable. They provide a clear way to manage configurations, deployment and orchestration. Each Playbook can run multiple Ansible tasks and you can chain them together to accomplish complex workflows.
Structure of a Playbook
An Ansible Playbook is structured into several key sections :-
1. Hosts
The hosts
section specifies the target machines where the Playbook tasks will run. Hosts can be grouped into different categories and you can specify individual hosts or groups.
2. Tasks
Tasks are the basic units of action in a Playbook. Each task performs a specific action such as installing a package, creating a file or restarting a service. Tasks are executed sequentially.
3. Variables
Variables allow you to store values that can be reused throughout the Playbook. They help make your Playbooks more flexible and reusable.
4. Handlers
Handlers are similar to tasks but are only run when notified by other tasks. They are typically used for operations that need to occur only when a change has been made such as restarting a service after a configuration file has been modified.
Writing a Simple Playbook to Install a Package
Let's write a simple Ansible Playbook to install the Apache web server on a group of web servers. We'll cover each part of the Playbook in detail.
Example Playbook : Install Apache
---
- name: Install Apache on web servers
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
Explanation :-
Playbook Header (
---
) :- The---
line indicates the beginning of a YAML file.Name (
- name: Install Apache on web servers
) :- Thename
field provides a description of the Playbook's purpose.Hosts (
hosts: webservers
) :- Thehosts
field specifies that the Playbook should run on the group of hosts namedwebservers
. This group should be defined in your inventory file.Privilege Escalation (
become: yes
) :- Thebecome
field allows the tasks to be executed with elevated privileges (e.g., using sudo).Tasks :-
Task Name (
- name: Install Apache
) :- Each task begins with a name, describing what the task does.Task Action (
apt
) :- Theapt
module is used to manage packages on Debian-based systems.Module Parameters :-
name: apache2
specifies the package to be installed.state: present
ensures the package is installed.
Inventory File
Before running the Playbook, you need to define an inventory file that lists the target hosts. Here's a simple example :-
# inventory.ini
[webservers]
webserver1 ansible_host=192.168.1.10
webserver2 ansible_host=192.168.1.11
Running the Playbook
To execute the Playbook, use the ansible-playbook
command, specifying the inventory file and the Playbook file.
ansible-playbook -i inventory.ini install_apache.yml
Output
When you run the Playbook, Ansible will connect to the hosts defined in the webservers
group, elevate privileges and execute the tasks to install Apache. The output will show the status of each task indicating whether changes were made or if the system was already in the desired state.
PLAY [Install Apache on web servers] *************************************
TASK [Gathering Facts] ***************************************************
ok: [webserver1]
ok: [webserver2]
TASK [Install Apache] ****************************************************
changed: [webserver1]
changed: [webserver2]
PLAY RECAP ***************************************************************
webserver1 : ok=2 changed=1 unreachable=0 failed=0
webserver2 : ok=2 changed=1 unreachable=0 failed=0
In this example, changed=1
indicates that the Apache package was installed successfully on both webserver1
and webserver2
.
Adding Variables and Handlers
Let's enhance our Playbook by adding variables and handlers.
Example Playbook with Variables and Handlers
---
- name: Install and configure Apache on web servers
hosts: webservers
become: yes
vars:
apache_package: apache2
apache_service: apache2
tasks:
- name: Install Apache
apt:
name: "{{ apache_package }}"
state: present
- name: Ensure Apache is running
service:
name: "{{ apache_service }}"
state: started
enabled: yes
- name: Create a simple HTML file
copy:
content: "<h1>Hello from Ansible</h1>"
dest: /var/www/html/index.html
notify:
- Restart Apache
handlers:
- name: Restart Apache
service:
name: "{{ apache_service }}"
state: restarted
Explanation :-
Variables (
vars
) :-apache_package
andapache_service
variables store the package and service names.
Tasks :-
Install Apache :- Uses the
apt
module to install the Apache package referencing theapache_package
variable.Ensure Apache is Running :- Uses the
service
module to start and enable the Apache service.Create a Simple HTML File :- Uses the
copy
module to create an HTML file and triggers a handler to restart Apache if the file is changed.
Handlers :-
- Restart Apache :- This handler restarts the Apache service when notified by the
Create a Simple HTML File
task.
- Restart Apache :- This handler restarts the Apache service when notified by the
Running the Enhanced Playbook
ansible-playbook -i inventory.ini install_and_configure_apache.yml
Output
PLAY [Install and configure Apache on web servers] ***********************
TASK [Gathering Facts] ***************************************************
ok: [webserver1]
ok: [webserver2]
TASK [Install Apache] ****************************************************
changed: [webserver1]
changed: [webserver2]
TASK [Ensure Apache is running] ******************************************
ok: [webserver1]
ok: [webserver2]
TASK [Create a simple HTML file] *****************************************
changed: [webserver1]
changed: [webserver2]
RUNNING HANDLER [Restart Apache] *****************************************
changed: [webserver1]
changed: [webserver2]
PLAY RECAP ***************************************************************
webserver1 : ok=5 changed=3 unreachable=0 failed=0
webserver2 : ok=5 changed=3 unreachable=0 failed=0
Conclusion
In this guide we've covered the basics of Ansible Playbooks including their structure and key components. We wrote a simple Playbook to install the Apache web server and demonstrated how to run it. We then enhanced the Playbook by adding variables and handlers, showcasing how to make your Playbooks more flexible and reusable.
By mastering Ansible Playbooks you can automate complex tasks, ensure consistency across your infrastructure and save valuable time.
Let's continue exploring the capabilities of Ansible in future posts diving deeper into advanced topics and real-world use cases to further enhance your automation skills.