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
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard.
Click "Launch Instance" and select an Amazon Machine Image (AMI) of your choice.
Choose an instance type and configure the instance details.
Configure instance storage, add tags, and set up security groups.
Review and launch the instances.
Step 2: Setting Up Ansible on the Host Server
Choose an EC2 instance to act as the Ansible host.
Connect to the host instance using SSH.
Update the package repository using
sudo apt update
(for Ubuntu) or the appropriate command for your distribution.Install Ansible using
sudo apt install ansible
.
Step 3: Creating the Ansible Playbook
Create a directory for your playbook files.
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
Open the terminal on the Ansible host.
Navigate to the directory containing the playbook file.
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.