Managing Complexity: Exploring Ansible's Multiple Inventories and Playbooks for Efficient Automation
Follow this blog to know more about Ansible and How to connect the Ansible master server with the node servers.
In this article, we will be looking into Ansible deployment files and playbook:
Create a folder named ansible and inside that folder create another folder named inventories. Here we created a prod inventory
Next copy anyone of the Node server's IP addresses for the prod inventory
Next, create a prod inventory file using Vim editor and add servers syntax and add the IP address, and also add Python interpreter and ssh private key location path
We can ping the prod server to test if its working or not
Next, we create another inventory for dev inventory same as prod but here we add a different node server IP address. That is how we create different inventories for different environments and servers
Next, go back to the Ansible folder and create another folder there for Playbooks.
Playbooks in Ansible are files that contain a set of instructions, known as tasks, written in YAML format. They define the desired state of a system and how Ansible should configure or manage it. Playbooks provide a way to automate complex tasks, orchestrate multiple steps, and perform configuration management. They are the heart of Ansible and allow users to define the desired infrastructure state and execute tasks in a declarative manner.
Here we create a date.yml file which will show the date on every server
The syntax for playbook yaml file this file first start with "-"
After creating the file we execute the file using the command ansible-playbook date.yml
, In the command, we commanded every server to execute the playbooks.
For practical purposes connect to anyone Node server. Here we connected to Node server -1
Next, we create another playbook to install Nginx in node servers
To execute the Nginx playbook we run the same command as before to install Nginx in all the node servers
Next, to check if nginx is installed, Copy the Public IP of the Node server - 1 and open in a new tab
Here we can see the nginx welcome page, which means that the installation was successful
Next, we create a conditional yml playbook. where we can instruct to execute the playbook in a specific operating System (OS)
In conditional yml file, we add a task for docker installation. And added the "when" condition for CentOS and "Red hat"
On "apt", - name = docker.io" ( I forgot to add docker.io in the - name entry after "apt" but corrected it later on )
Next, we execute the condition yml file and we can see that the Docker installation is skipped for all three servers because all three servers are Ubuntu-based OS
Next, on the same condition yml file, we edit another "when" the condition for AWS CLI installation for the ansible distribution is "Debian" or "Ubuntu" Based OS
Again we execute the condition playbook and it first skipped the first "when" condition for "CentOS" and "Red Hat" and executed the installation for the second "when" condition to install "AWS CLI" in all the Node servers
In Node server - 1, we can check that AWS CLi is successfully installed.
Next, In the same condition, the playbook removes the "when" condition for "CentOS and Red Hat" to install Docker on Node servers that have "Ubuntu OS" and add docker.io in the name entry
Next, execute the condition playbook to install docker in all the node servers
Next, we create an Index.html file in the playbook folder, so that we can add an index.html file to the deploy_webpage.yml file later on
Next, we create a deploy_webpage.yml file where we add syntax to install Nginx and add index.html location path.
The path "/var/www/html" is commonly used as the default location for serving web content on a Linux system. In the case of Nginx, this path is typically configured as the root directory for hosting static web files. By placing an "index.html" file in this directory, Nginx will be able to serve that file as the default page when accessing the web server. This allows users to access the website by simply entering the server's IP address or domain name in a web browser.
The content of deploy_webpage.yml
Next, we execute the deploy_webpage.yml file to a specific inventory "prod"
The purpose of deploying webpage file is to demonstrate that we can execute different tasks and playbooks for specific inventories and environment
Now, As we know that the prod inventory has the Node server - 1 IP Address, Now copy the IP address of the node server - 1 and open it in a new tab, here we can see that the webpage is been deployed.
Thanks for investing your valuable time to read my Blog!!!!