๐Ÿ”Ž Ansible Project: Automating Infrastructure Deployment ๐Ÿš€

๐Ÿ”Ž Ansible Project: Automating Infrastructure Deployment ๐Ÿš€

Day 59 : #90DaysOfDevOps Challange

ยท

3 min read

Learning of Day 59: Today, we will dive into the world of Ansible, a powerful automation tool for managing and configuring systems. In this tutorial, we will guide you through the process of setting up an Ansible environment and using it to deploy Nginx and a sample webpage on three EC2 instances. Let's get started! ๐Ÿ’ช

Step 1๏ธโƒฃ: Creating EC2 Instances To begin, create three EC2 instances using the Ubuntu t2.micro instance type. Make sure all three instances are associated with the same key pair. This key pair will allow you to access the instances securely. ๐Ÿ–ฅ๏ธ๐Ÿ–ฅ๏ธ๐Ÿ–ฅ๏ธ

Step 2๏ธโƒฃ: Installing Ansible Now, let's install Ansible on the host server. You can use the following commands to install Ansible on Ubuntu:

sudo apt update
sudo apt install ansible

Once the installation is complete, Ansible will be available on your system. โœ”๏ธ

Step 3๏ธโƒฃ: Copying Private Key To enable communication between the Ansible host and the EC2 instances, we need to copy the private key from your local machine to the Ansible host. Use the following command to copy the private key to the /home/ubuntu/.ssh directory on the Ansible host:

scp -i /path/to/your/key.pem /path/to/your/key.pem ubuntu@ansible_host_ip:/home/ubuntu/.ssh/

Make sure to replace /path/to/your/key.pem with the actual path to your private key file and ansible_host_ip with the IP address of your Ansible host. ๐Ÿ”‘

Step 4๏ธโƒฃ: Accessing the Inventory File Next, we need to configure the inventory file, which lists the hosts Ansible will manage. Access the inventory file using the following command:

sudo nano /etc/ansible/hosts

In the inventory file, add the IP addresses or hostnames of the three EC2 instances under a suitable group name, such as [web-servers]. Save and exit the file. ๐Ÿ“

Step 5๏ธโƒฃ: Creating the Playbook It's time to create a playbook that will install Nginx on the EC2 instances. Create a file named nginx-playbook.yml and add the following content:

-
 name: This playbook will install nginx
 hosts: servers
 become: yes
 tasks:
  - name: install nginx
    apt:
      name: nginx
      state: latest
  - name: start nginx
    service:
      name: nginx
      state: started
      enabled: yes

Save the playbook file. This playbook specifies that it should be executed on the hosts in the servers group and installs Nginx using the apt module. ๐ŸŒ

Step 6๏ธโƒฃ: Deploying a Sample Webpage Now, let's deploy a sample webpage on the EC2 instances using the Ansible playbook. Modify the nginx-playbook.yml file and add the following task at the end:

    - name: Deploy sample webpage
      copy:
        src: /path/to/your/sample-webpage/index.html
        dest: /var/www/html/index.html
      become: true
      become_user: root
      become_method: sudo

Replace /path/to/your/sample-webpage/index.html with the actual path to your sample webpage file. This task copies the index.html file to the default Nginx web server

directory. ๐Ÿ“„

Step 7๏ธโƒฃ: Running the Playbook To run the playbook and deploy Nginx with the sample webpage, use the following command:

ansible-playbook nginx-playbook.yml

Ansible will connect to the EC2 instances, install Nginx, and deploy the sample webpage. Once the playbook execution is complete, you can access the webpage by entering the IP addresses or hostnames of the EC2 instances in a web browser. ๐ŸŒ

Congratulations! You have successfully set up an Ansible environment, installed Nginx, and deployed a sample webpage on multiple EC2 instances. Keep exploring Ansible's vast capabilities to automate and streamline your infrastructure management tasks! ๐ŸŽ‰

That's it for today's learning adventure! We hope you enjoyed it. Stay tuned for more exciting tutorials. Happy automating! ๐Ÿค–โœจ

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

ย