Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using ansible, sounds like a good project, right?
Task-01
create 3 EC2 instances . make sure all three are created with same key pair
Install Ansible in host server
copy the private key from local to Host server (Ansible_host) at (/home/ubuntu/.ssh)
access the inventory file using sudo vim /etc/ansible/hosts
Create a playbook to install Nginx
deploy a sample webpage using the ansible playbook
Create 3 EC2 instances. make sure all three are created with the same key pair.
To set up three EC2 instances, go to the EC2 service and initiate their launch.
After successfully creating the trio of EC2 instances, identify one as the Ansible control node (ansible_host) and the remaining two as managed nodes (ansible_server).
Install Ansible on the control node:
Establish an SSH connection to your EC2 instance.
Add the Ansible PPA repository with the command:
sudo apt-add-repository ppa:ansible/ansible
Update the package using the following commands:
sudo apt-get update
Install Ansible:
sudo apt-get install ansible
Upon completion of the installation, verify the Ansible version:
ansible --version
Transfer the private key from your local machine to the Ansible control node (ansible_host) at the location (/home/ubuntu/.ssh).
To proceed with the configuration, move the private key to the Ansible control node (ansible_host) by creating a new file at /home/ubuntu/.ssh and pasting the private key into that file.
Grant appropriate permissions to the private key file using the chmod command.
Access the inventory file using:
sudo vim /etc/ansible/hosts
Create an inventory file at the default location (/etc/ansible/hosts), which contains a list of hosts or servers. Add the IP addresses of the servers and specify the location of the private key file for authentication.
Craft a playbook to install Nginx:
Specify the playbook's name (Install nginx) and the target hosts (servers). Include become: yes to execute tasks with root privileges. The first task updates the apt cache on the managed nodes using the apt module. The second task installs the latest Nginx version using the same module. The third task starts the Nginx service using the service module, specifying the service name (nginx) and setting the state parameter to started.
Execute the playbook using the ansible-playbook command:
ansible-playbook file-name.yml
Inspect the Nginx status on the two EC2 instances.
Deploy a sample webpage using the Ansible playbook:
Create a new file, index.html, in the playbook directory and add sample content.
Update an Ansible playbook file, install_nginx.yml, in the playbook directory:
This playbook copies the index.html file to the default Nginx web server document root directory at /var/www/html/.
Execute the playbook.
Once the playbook completes execution, open a web browser and enter the public IP address of one of the EC2 instances running Nginx.