Day 59 - Ansible Project π₯
Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using Ansible? Sounds like a good project, right? Letβs get started!
Task-01: Create 3 EC2 Instances π₯οΈβοΈ
Log in to AWS Management Console π₯οΈ
Navigate to EC2 Dashboard β‘οΈ
Launch Instance:
Choose the Amazon Machine Image (AMI) (e.g., Ubuntu Server 20.04 LTS) π§.
Select the instance type (e.g., t2.micro for free tier) πΈ.
Configure instance details:
Number of instances: 3
Network settings: Default VPC π.
Add Storage: Leave the default settings π¦.
Add Tags: (Optional) Tag your instances for easy identification π·οΈ.
Configure Security Group:
Add a rule to allow SSH (port 22) from your IP π.
Add a rule to allow HTTP (port 80) from anywhere π.
Review and Launch: Use the same key pair for all three instances (create a new one if you donβt have any) ποΈ.
Install Ansible on Host Server π οΈ
Connect to Your Host Server:
- Open terminal and SSH into your EC2 instance (one of the three you just created) π».
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-instance-public-dns
Update Package Lists π:
sudo apt update
Install Ansible π¦:
sudo apt-add-repository ppa:ansible/ansible sudo apt install ansible -y sudo ansible --version
Copy the Private Key to Host Server (Ansible_host) π
Transfer the Private Key:
- From your local machine, copy the key to the host server using SCP π.
escp -i /path/to/your-key-pair.pem /path/to/your-key-pair.pem ubuntu@your-ec2-instance-public-dns:/home/ubuntu/.ssh/
Set Permissions on the Key:
- SSH into your host server π».
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-instance-public-dns
- Change permissions of the private key π.
chmod 600 /home/ubuntu/.ssh/ansible_keys.pem
Access the Inventory File π
Edit the Ansible Hosts File βοΈ:
sudo vim /etc/ansible/hosts
Add Your EC2 Instances:
- Add the IP addresses of your EC2 instances under a new group called
[webservers]
π₯οΈ.
- Add the IP addresses of your EC2 instances under a new group called
[servers]
server01 ansible_host=3.136.18.176
server02 ansible_host=3.137.211.34
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_keys
Create a Playbook to Install Nginx π
Create Playbook File:
- On your host server, create a new playbook file π.
vim install_nginx.yml
Write the Playbook:
--- - name: Install Nginx on web servers hosts: webservers become: yes tasks: - name: Ensure Nginx is installed apt: name: nginx state: present - name: Start Nginx service service: name: nginx state: started enabled: yes - name: Deploy sample web page copy: content: "<h1>Welcome to your Ansible-deployed web server!</h1>" dest: /var/www/html/index.html
Deploy the Sample Webpage Using the Ansible Playbook π
Run the Playbook π:
ansible-playbook install_nginx.yml
Happy Learning! ππ
Subscribe to my newsletter
Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nilkanth Mistry
Nilkanth Mistry
Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement