Table of contents
- Task-01
- create 2 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
Task-01
create 2 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
Step 1:- First we need to create 2 EC2 instances . make sure all three are created with same key pair
Step 2:- Now Install Ansible in host server so for that Need to go to the machine and make sure we have updated it
Need to Ansible below command for installing Ansible in the master machine
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Now let's check whether Ansible is installed or not
Step 3:- copy the private key from local to Host server (Ansible_host) at (/home/ubuntu/.ssh)
Need to create new file and past it there
need to give the file permission using below command
sudo chmod 600 ansible_key
**Step 4:-**Now we need to go /etc/ansible/hosts which is by default the location of file. An Ansible hosts file is a configuration file that contains a list of hosts or servers.
[servers]
server1 ansible_host=54.147.179.71
server2 ansible_host=54.196.183.147
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key
After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command.
ansible-inventory --list -y
Now we need to ping using below command
ansible all -m ping
is used to test connectivity to all hosts in the Ansible inventory using the ping
module. It sends a ping command to each host and checks if the host is reachable.
ansible all -m ping -i /home/ubuntu/ansible/hosts
Step 5:- Now we need to Create a playbook to install Nginx
---
- name: This playbook will install nginx
hosts: servers
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
enabled: yes
Run playbook using the ansible-playbook command
ansible-playbook file-name.yml -i /home/ubuntu/ansible/hosts
Ex:- ansible-playbook install_ngnix.yml -i /home/ubuntu/ansible/hosts
Check the status of Nginx on the two EC2 instances,
ansible servers -a "sudo systemctl status nginx" -i /home/ubuntu/ansible/hosts
Step 6:- 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:-
---
- name: This playbook will install nginx
hosts: servers
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
- name: start nginx
service:
name: nginx
state: started
enabled: yes
- name: Deploy webpage
copy:
src: index.html
dest: /var/www/html/index.html
We need to 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
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee