Day 59: Ansible Project 🔥

·

2 min read

Day 59: Ansible Project 🔥

Task-01

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

Install Ansible on a host server

Connect to your EC2 instance using SSH.

Add the Ansible PPA repository using the following command:

sudo apt-add-repository ppa:ansible/ansible

Update the package using the following commands.

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

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

Copy the private key from your local.

Give permissions to the private key file using chmod command.

sudo chmod 600 ansible_key

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

Create inventory file at location /etc/ansible/hosts which is by default location of file. 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.

Create a playbook to install Nginx

In this playbook, we first specify the name of the playbook (Install nginx) and the hosts we want to target (servers). We also set become: yes to run the 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 version of Nginx using the same module. The third task uses the service module to start the Nginx service by specifying the service name (nginx) and setting the state parameter to start.

Run playbook using the ansible-playbook command

ansible-playbook file-name.yml

Check the status of Nginx 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 some sample content

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

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

Run the playbook

Once the playbook finishes executing, open a web browser and enter the public IP address of one of the EC2 instances running Nginx

I hope you find this article helpful!

Happy Learning!

Â