Day 2 - Creating Ansible Playbooks
Introduction
Definition of Ansible Playbooks
Ansible Playbooks are the YAML files that define a set of tasks to be executed on managed nodes. They are used to describe automation workflows in a human-readable format.
Importance of Ansible in IT Automation
Ansible Playbook follows a declarative approach, instead of specifying step-by-step instructions which is an imperative approach, the desired end result is described. To simplify, consider if we are required to have a service in a running state or a package to be installed, and then Ansible will handle all the steps of how the desired state can be achieved.
Getting Started with Ansible
Installing Ansible
Reference to Ansible installation guide
Writing the First Ansible Playbook
Creating the YAML File
Let’s create a simple Ansible playbook and execute the same:
We will create a sample txt file on localhost which is the Master node of Ansible copy it to the host nodes using Ansible-playbook
sudo vi copy_file.yml
Checking the syntax of the playbook:
ansible-playbook --syntax-check copy_file.yml
Make sure the host instances are running:
Adding Inventory
Verify if the hostnames are present in the inventory file on the Ansible machine:
Running the Playbook
Error Handling and Debugging
Since the execution permission was not given, the playbook didn’t get successfully executed and the process was stuck indefinitely.
Make sure that the execution permission is given to the playbook.
chmod +x playbook.yml
Output
The file1.txt file is visible on host machines
Nginx and Apache2 Installation using Playbook
Now, let’s try installing Nginx and Apache on server01 and server02 respectively
Verifying if the ssh connections to the hosts are intact
ansible all -m ping
Checking the playbook syntax
ansible-playbook --syntax-check installer.yml
yaml code:
Running the Playbook
ansible-playbook installer.yml
Nginx and Apache webservers are successfully installed on server01 and server02
If we navigate with the public IPs of the servers, we will land on the default pages of Nginx and Apche2 webservers.
Conclusion
In this Blog, we have seen how to install and configure the remote servers using Ansible-playbooks. Let’s dive deep and explore some more functionalities of the configuration management tool in the upcoming blog series!!
Subscribe to my newsletter
Read articles from Akash Sutar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by