Deploying Nginx with Ansible on AWS EC2 Instances.

Deploying Nginx with Ansible on AWS EC2 Instances.

Are you ready to dive into the world of infrastructure automation and management? In this blog post, we'll walk you through the process of creating three EC2 instances on Amazon Web Services (AWS), setting up Ansible on the host server, and creating a playbook to install Nginx on those instances. Let's get started!

Step 1: Creating EC2 Instances

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 Dashboard.

  3. Click "Launch Instance" and select an Amazon Machine Image (AMI) of your choice.

  4. Choose an instance type and configure the instance details.

  5. Configure instance storage, add tags, and set up security groups.

  6. Review and launch the instances.

Step 2: Setting Up Ansible on the Host Server

  1. Choose an EC2 instance to act as the Ansible host.

  2. Connect to the host instance using SSH.

  3. Update the package repository using sudo apt update (for Ubuntu) or the appropriate command for your distribution.

  4. Install Ansible using sudo apt install ansible.

Step 3: Creating the Ansible Playbook

  1. Create a directory for your playbook files.

  2. Inside the directory, create a file named nginx_install.yml.

yamlCopy code---
- name: Install Nginx on EC2 Instances
  hosts: all
  become: true

  tasks:
    - name: Update apt package cache
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: latest

Step 4: Running the Playbook

  1. Open the terminal on the Ansible host.

  2. Navigate to the directory containing the playbook file.

  3. Run the playbook using the command: ansible-playbook -i <path-to-inventory-file> nginx_install.yml

Conclusion

Congratulations! You've successfully created EC2 instances on AWS, set up Ansible on the host server, and used an Ansible playbook to install Nginx on those instances. This process demonstrates the power of automation in managing and deploying infrastructure components. As you explore further, you can expand your playbook to include more complex configurations and applications.