🚀 Ansible Project 🔥

Trushid HatmodeTrushid Hatmode
3 min read

Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using Ansible, sounds like a good project, right?

  1. Create 3 EC2 instances. make sure all three are created with the same key pair.

Navigate to the EC2 service and launch three EC2 instances.

The three EC2 instances should be created, with one being designated as the Ansible control node (ansible master) and the other two as managed nodes (node1 and node2).

No alt text provided for this image

  1. Install Ansible on the host server

Connect to your EC2 instance using SSH.

No alt text provided for this image

Add the Ansible PPA repository using the following command:

sudo apt-add-repository ppa:ansible/ansible

No alt text provided for this image

Update the package

No alt text provided for this image

Install Ansible using the following command:

sudo apt install ansible

Once the installation is complete, you can check the version of Ansible using the following command:

ansible --version

No alt text provided for this image

Copy the private key from the local to the Host server (ansible master) at (/home/ubuntu/.ssh)

Copy the private key from your local.

No alt text provided for this image

Create a new file at /home/ubuntu/.ssh, paste the private key in it, and then continue with the setup by copying the private key to the Ansible control node (ansible master).

No alt text provided for this image

Give permissions to the private key file using chmod command.

No alt text provided for this image

In case of facing any error, you can elevate the permission of the key*( eg: sudo chmod 666 key_name)*

Access the inventory file using sudo nano /etc/ansible/hosts

Create inventory file at location /etc/ansible/hosts which is by default location of the file. An Ansible hosts file is a configuration file that contains a list of hosts or servers. Add the IP addresses of the servers and also add private key file locations to use for authentication.

No alt text provided for this image

Create a playbook to install Nginx

No alt text provided for this image

In this playbook, we first give the hosts we want to target as well as the playbook's name (Install nginx). (servers). In order to start the tasks with root permissions, we also set become: yes.

The first task uses the apt module to update the apt cache on the controlled nodes. Using the same module, the second task installs the most recent version of Nginx. The third step starts the Nginx service by supplying the service name (nginx) and setting the status parameter to start using the service module.

Use the ansible-playbook command to execute a playbook.

ansible-playbook file-name.yml

No alt text provided for this image

Check the status of Nginx on the two EC2 instances,

No alt text provided for this image

Deploy a sample webpage using the Ansible playbook

Create a new file index.html in the playbook directory, and add some sample content

No alt text provided for this image

Update ansible playbook file, install_nginx.yml, in the playbook directory:

No alt text provided for this image

This playbook will copy the index.html file to the default Nginx web server document root directory at /var/www/html/.

Run the playbook

No alt text provided for this image

Open a web browser and type the public IP address of one of the EC2 instances running Nginx after the playbook has finished running.

No alt text provided for this image

The website will be present on all the servers, so check them all!

No alt text provided for this image

You can add more layers or improvise more on your own; add this project to your résumé! Share it with your peers if it even slightly expands your understanding🙌❤️

1
Subscribe to my newsletter

Read articles from Trushid Hatmode directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Trushid Hatmode
Trushid Hatmode

As a DevOps Engineer, I'm passionate about building and maintaining robust, efficient, and scalable infrastructure to enable seamless software delivery. With a strong foundation in C/C++, Linux, and a toolkit that spans AWS, Jenkins, Docker, Nagios, Kubernetes, YAML, Ansible, Terraform, Bash Scripting, Git, and GitHub, I'm well-equipped to tackle the challenges of modern software development and deployment.