What is Ansible Playbook and Roles?

Ansible Playbook is a YAML-formatted script defining server configuration tasks. Executed sequentially by Ansible, it enables the creation of complex, repeatable, and scalable automation workflows.

Moreover, Playbook facilitates sending scripted commands to remote computers, enabling remote configuration via Ansible commands. This versatility allows for executing simple tasks like sequential server restarts or complex operations such as deploying numerous VMs across private and public clouds.

Ansible Playbook

 

How Ansible Playbook Works?

An Ansible Playbook outlines the desired system state, executing a series of tasks to achieve it. Comprising one or more plays, each play consists of sequentially executed tasks on target machines, completing the playbook when all tasks and plays are done.

Playbooks leverage modules for actions on target hosts, containing functionalities like file management, package installation, and configuration manipulation.

 

Building Blocks of an Ansible Playbook include:

Hosts:

Listing servers/computers for playbook execution, sourced from Ansible’s inventory file. Hosts can be defined globally, for specific host groups, or within tasks, enabling targeted actions and configurations.

 

Variables:

Storing values accessible throughout the playbook, defined globally, for specific hosts/groups, or within tasks. Variables enhance playbook flexibility, adapting to diverse environments and scenarios. They aid in making playbooks dynamic and adaptable.

 

Defining Tasks in Ansible Playbooks:

Now, create a playbook definition file, vim main.yml. The file will contain a list of all the tasks required to install and start the Apache service on the target host mentioned in the inventory file.

vim main.yml

Add the below code in the main.yml file.

 

The first step in every playbook is to define the hosts. In our inventory.ini file, we have assigned the label ‘clients’ to represent the hosts. Inside the YAML file, we have outlined three tasks: updating packages using the apt module, installing Apache, and starting the Apache service. To run the playbook, you can utilize the ansible playbook command as shown below. This command will establish a connection with the hosts mentioned in the inventory.ini file and execute the tasks defined in main.yml.

 

Setup Ansible with Server:

Ansible links to managed nodes and deploys lightweight applications known as Ansible modules. These applications represent the intended configuration of the system. Ansible subsequently runs these modules, typically via SSH, and deletes them once the task is complete.

To use the ansible, you need a dedicated server or VPS server with SSH access to install and configure it. With our dedicated server, you can have full control over the server and do any process or application you want in the server through terminal.

Also Read: Understanding Ansible Installation and Usage Guide

 

Conclusion:

The article has provided an overview of Ansible Playbooks and their key elements. By following an example, you have gained insight into creating and running playbooks. Yet, this is just scratching the surface of what this robust tool can achieve.