“Automating Your Infrastructure: A Step-by-Step Guide to Installing Ansible on Ubuntu and Master-Slave Operations”
Introduction to Ansible:
Ansible is an open-source automation tool that simplifies the management, configuration, and deployment of systems and applications. It is designed to make IT tasks more manageable, efficient, and repeatable through the use of simple, declarative, and human-readable YAML scripts called “playbooks.”
Why Use Ansible:
- Easy to Learn and Use: Ansible’s simplicity allows both beginners and experienced sysadmins to quickly get started with automation tasks. Its YAML-based syntax makes it human-readable and reduces the learning curve.
- Agentless Architecture: Unlike many other configuration management tools, Ansible follows an agentless approach. It communicates with remote systems using SSH, reducing the need for additional software on the target machines.
- Idempotent Nature: Ansible ensures that tasks are idempotent, meaning the state of the system is determined based on the desired configuration rather than the steps needed to reach that state. This allows for safer and repeatable automation.
- Wide Range of Use Cases: Ansible can be used for a variety of tasks, such as system provisioning, configuration management, application deployment, cloud infrastructure management, and more.
Blog Context:
In this blog, we will walk you through a step-by-step guide on how to install Ansible on an Ubuntu system. We’ll cover the necessary prerequisites, installation methods, and verification steps to ensure Ansible is up and running smoothly.
Additionally, we’ll explore some basic operations of Ansible, focusing on the master-slave setup. We’ll learn how to set up an Ansible control node (master) and remote hosts (slaves), allowing us to manage multiple systems from a centralized location.
By the end of this blog, you’ll have a solid understanding of Ansible’s fundamentals and how to orchestrate basic tasks between the master and slave machines.
Upcoming Blog Sneak Peek:
In the next blog, we’ll take our Ansible skills to the next level by using Ansible Playbooks. Ansible Playbooks are powerful automation scripts that allow you to define the desired state of your infrastructure and applications. We’ll use Ansible Playbooks to deploy a website on multiple servers effortlessly. Stay tuned for an exciting hands-on tutorial that will make your infrastructure deployment a breeze!
1. Login to your AWS Account and then Launch 4 EC2 Instance t2.micro with OS ubuntu and default security group in 4 instance 1 is Master Node and other 3 are Slave Node and Make sure all instance Keys are same
2. After successfully launch 4 Instance lets connect “Ansible-Master” to install ansible on that and configure other 3 Nodes
3. After login to your master instance first we update the instance & then install ansible on it
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y
4. Now lets verify ansible install or not
ansible --version
5. Now lets add our slaves server into master configuration so that master and slave communicate with each others so we need to go “ansible host file”
sudo vi /etc/ansible/hosts
6. After open this file you need to add Public IP of slaves nodes
[servers]
server_1 ansible_host=<Public IP>
server_2 ansible_host=<Public IP>
server_3 ansible_host=<Public IP>
7. Now When your launch your instance make sure the Key is same for all Instance. now for all server we use same Pem key so ansible master connect with them, so that we make one folder and in that folder you can paste the Pem file in below I show using ssh or you can use Winscp
mkdir keys
cd keys
8. If your connect with SSH then copy the Pem file to local to server for that follow below command
scp -i "Final1.pem" Final1.pem ubuntu@ec2-54-210-169-212.compute-1.amazonaws.com:/home/ubuntu/keys
9. Or else you can use Winscp to copy your Pem file to server
10. Go back again “/etc/ansible/hosts” & give the location of that key
[servers:vars]
ansible_ssh_private_key_file=/home/ubuntu/keys/Final1.pem
11. Then add ansible python interpreter & particular user (because ansible use Python)
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
12. so as you see we add group our servers and under that we add variables
13. now let ping that server so we can verify its accessible from host machine
ansible servers -m ping
14. now if you want to check ram of all server so with the help of ad-hoc command we can check
ansible servers -a "free -h"
15. now if we need to update the all servers for that
ansible servers -a "sudo apt update"
16. now if you want update particular server then create a another group and add that server on that group so you need to modify again hots file of ansible
vi /etc/ansible/hosts
17. now we add 3rd server on another group name as prod and save the file and Ping that server again
ansible prod -m ping
18. Now revert the changes again
19. if you want to see available or attach server you can use ansbile-inventory
ansible-inventory --list
20. Now this is Installation of Ansible on Ubuntu. We configure Master-Slave configuration now in next blog we will see with the help of Ansible-Playbook how we can deploy a website so stay tuned
21. After the Practical Make sure terminate all instance to avoid unnecessary billing