Hey Learners! Welcome back. As we understand the concept of Ansible playbooks and Ad-hoc commands. In this challenge, we'll do a simple project which includes connecting 2 nodes as Ansible nodes and will deploy a static webpage using NGINX on managed hosts using the Ansible playbook. Let's start...
Task 1-
Create 3 EC2 instances. Make sure all three are created with the same key pair
Install Ansible on the host server
Log in to the AWS console and Search for an EC2 instance to create 3 instances. Select Launch instances and create 3 instances with the same key pair.
I already have 3 instances created before and Ansible installed on one of the instances called Ansible Master. To do the same follow the the steps mentioned in Challenge Day55. Click here to see the blog
Check the version of Ansible and do a ping-pong test by executing the Ad-hoc command.
Create a playbook to install Nginx
Create a file nginx_install.yml with the content shown below.
---
- name: Install NGINX and Start NGINX service
hosts: nodes
become: yes
tasks:
- name: Updating package manager first
apt:
update_cache: yes
- name: Intsalling NGINX
apt:
name: nginx
state: latest
- name: Start NGINX service
service:
name: nginx
state: started
enabled: yes
...
The above playbook includes 3 tasks Update package manager, install nginx and start nginx service on managed nodes.
Execute the playbook and check the status of the Nginx service.
Nginx service is running on both managed nodes.
Deploy a sample webpage using the ansible-playbook
Now we can deploy static webpage using the Ansible playbook
Create an index.html file in which source code is present.
Now we will make changes to the nginx_install file to create a new task for deployment of the index.html file over the Nginx server or we can create a new YAML file to do the deployment.
Here we use handlers
as handlers used to execute the task only if there is any change happens. Using the notify
module to trigger the execution of the task(s) specified in the handler section.
Note argument provided for the notify
module should be the same as the name provided in the handlers
section.
---
- name: Install NGINX and Start NGINX service
hosts: nodes
become: yes
tasks:
- name: Updating package manager first
apt:
update_cache: yes
- name: Intsalling NGINX
apt:
name: nginx
state: latest
notify: restart_nginx
- name: Deploying Static webpage
copy:
src: index.html
dest: /var/www/html/index.html
notify: restart_nginx
handlers:
- name: restart_nginx
service:
name: nginx
state: restarted
...
Now execute the playbook modified.
Confirm that the webpage is deployed successfully by accessing the PublicIP of nodes over the browser as shown.
We successfully deployed a static webpage with the help of Ansible Playbooks on managed hosts at a time.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!