Day 59 - Ansible Project ??

Day 59 - Ansible Project ??

Automating Nginx Deployment on AWS EC2 Instances with Ansible

Introduction: In today's fast-paced world, automation plays a crucial role in managing and deploying applications efficiently. In this article, we will walk through the process of creating three Amazon EC2 instances, ensuring they share the same key pair, installing Ansible on the host server, and using Ansible to deploy and configure Nginx on these instances.

Step 1: Create EC2 Instances

1.1 Amazon EC2 Instance Creation:


  • Open the AWS Management Console.
  • Navigate to the EC2 dashboard.
  • Launch three instances, making sure to select the same key pair for each.


Step 2: Install Ansible on Host Server

2.1 Accessing the Host Server:


  • Connect to the host server using SSH.


ssh -i path/to/your-key-pair.pem ubuntu@your-host-ip        

2.2 Installing Ansible:


  • Update the package list:


sudo apt update        


  • Install Ansible:


sudo apt install ansible        

Step 3: Copy Private Key to Host Server

3.1 Copy Private Key:


  • Copy the private key from your local machine to the Ansible host server.


scp -i path/to/your-key-pair.pem path/to/your-key-pair.pem ubuntu@your-host-ip:/home/ubuntu/.ssh        

Step 4: Configure Ansible Inventory

4.1 Accessing Inventory File:


  • Open the Ansible inventory file using a text editor with sudo privileges.


sudo vim /etc/ansible/hosts        

4.2 Update Inventory File:


  • Add the private IP addresses of the EC2 instances to the inventory file.


[web_servers]
instance1_private_ip
instance2_private_ip
instance3_private_ip        

Step 5: Create Ansible Playbook for Nginx

5.1 Creating Ansible Playbook:


  • Create a new Ansible playbook file, e.g., nginx_install.yml.


---
- hosts: web_servers
  become: true
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
    - name: Start Nginx service
      service:
        name: nginx
        state: started        

5.2 Running Ansible Playbook:


  • Execute the Ansible playbook to install Nginx on the EC2 instances.


ansible-playbook -i /etc/ansible/hosts nginx_install.yml        

Step 6: Deploy Sample Webpage

6.1 Creating Sample Webpage:


  • Create a simple HTML file, e.g., index.html, containing your desired content.


6.2 Updating Ansible Playbook:


  • Modify the Ansible playbook to copy the HTML file to the Nginx web root.


---
- hosts: web_servers
  become: true
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
    - name: Start Nginx service
      service:
        name: nginx
        state: started
    - name: Deploy Sample Webpage
      copy:
        src: path/to/index.html
        dest: /var/www/html/index.html        

6.3 Run Updated Ansible Playbook:


  • Execute the updated Ansible playbook to deploy the sample webpage.


ansible-playbook -i /etc/ansible/hosts nginx_install.yml        

Conclusion: By following these steps, you have automated the deployment of Nginx on multiple EC2 instances using Ansible. This approach enhances efficiency, scalability, and maintainability in managing web server configurations.


I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .

thank you : )

要查看或添加评论,请登录

Guduru Bharat Kumar的更多文章

社区洞察

其他会员也浏览了