This project demonstrates the setup and configuration of a multi-server environment using Ansible on AWS EC2. The goal of this project is to host an Apache server on multiple Ubuntu servers.
- AWS account with appropriate permissions to launch EC2 instances.
- Ansible installed on the master server.
-
Launch Ubuntu EC2 instances on AWS:
- Launch four instances with the following names: master, server-1, server-2, server-3.
- Ensure that all instances are launched with the same configuration, key pair, and security group.
-
Access the master server:
- Connect to the master server using SSH.
- Install Ansible on the master server.
-
Configure SSH access:
- In the path
./ssh
, create a file namedmy-key
. - Copy and paste the key pair that you used while launching the servers into this file.
- Run the following command:
$ cd ./ssh $ vi my-key # Copy and paste the key pair into the file, save, and exit.
- In the path
-
Configure inventory file:
- By default, the inventory file is stored in
/etc/ansible/hosts
. If it doesn't exist, create a new folder namedansible
and inside that folder, create the inventory file namedhosts
. - Run the following commands:
$ sudo mkdir /etc/ansible $ sudo vi /etc/ansible/hosts
- Add the details of the nodes in the following format:
[nodes] server-1 ansible_host=<server-1 IP address> server-2 ansible_host=<server-2 IP address> server-3 ansible_host=<server-3 IP address>
- By default, the inventory file is stored in
-
Check inventory information:
- Run the following command to display the inventory information:
$ ansible-inventory --list -y -i /etc/ansible/hosts
- Run the following command to display the inventory information:
-
Ping all nodes:
- Run the following command to ping all nodes:
$ ansible all -m ping -i /etc/ansible/hosts --private-key=~/.ssh/my-key
- Run the following command to ping all nodes:
-
Configure playbook:
- Create a file named
test.yaml
in the Ansible project directory. - Write the necessary Ansible playbook code to install and configure Apache on the target nodes.
- Run the following command:
$ vi test.yaml # Add the playbook code, save, and exit.
- Create a file named
-
Execute the playbook:
- Run the following command from the Ansible project directory:
$ ansible-playbook test.yml -i /etc/ansible/hosts --private-key=~/.ssh/my-key
- Run the following command from the Ansible project directory:
-
Verify the setup:
- Access the IP addresses of
server-1
,server-2
, andserver-3
in a web browser. - You should see the default Apache page indicating a successful deployment.
- Access the IP addresses of