Day 15 Project 3: Hosting Static Website using Ansible Playbook on AWS
Day 15
Create 4 Instances on AWS, One Ansible Master and 3 Slave nodes. Use same key pair for all instances while creating
Install Ansible on Master node
Edit host file on Master node
The hosts file is usually located at /etc/ansible/hosts
on the Ansible master node. Edit this file to specify the hostnames or IP addresses of the slave nodes you want to manage.
Ansible hosts file is crucial for defining the inventory of servers that Ansible will manage. It specifies how Ansible can connect to remote servers, including their IP addresses, hostnames, and any required connection parameters. It serves as the inventory of all the machines in your infrastructure.
In the context of connecting via SSH, the hosts file includes information such as:
Hostnames or IP addresses: This is how Ansible identifies the machines. You need to specify the target machines (slave nodes) in the hosts file.
Connection parameters: Ansible needs to know how to connect to each machine. This includes the username, SSH key file, and any other relevant parameters.
sudo vim /etc/ansible/hosts
Created folder to keep pem file
Copy pem file from local to Master node as it should connect with Salve node
Check file copied in specified path
Again edit host file
Now try to ping Slave nodes from master
ansible servers -m ping
Check information about system memory of Slave nodes from Master node
ansible servers -a "free -h"
Update Slave nodes from Master node
ansible servers -a "sudo apt update"
To make changes on particular server put it in separate group
We are getting the below error because ssh key information is not provided for "prod" group
ansible-inventory --list -> It helps you confirm that your inventory file is correctly configured or not
Provide variables to "all" groups in host file
ansible-inventory --list
Now, Master node is able to ping Prod group node separately
Ansible Playbook
An Ansible playbook is a YAML file where you define a set of tasks that Ansible will execute on remote hosts. Some key points about Ansible playbooks are:
YAML Format:
- Ansible playbooks are written in YAML (YAML Ain't Markup Language) format. YAML is a human-readable data serialization format.
Basic Structure:
A playbook consists of a list of plays, and each play contains a set of tasks.
At the top level, you might define hosts, become (sudo) settings, and other global configurations.
---
- name: My First Playbook
hosts: servers
become: true
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: Ensure Nginx is running
service:
name: nginx
state: started
Plays and Tasks:
A play is a set of tasks that are executed on a defined set of hosts. In the example above, the play is named "My First Playbook," and it targets the group "servers."
Each task performs a specific action, such as installing a package (
apt
module) or starting a service (service
module).
Modules:
Ansible modules are reusable, standalone scripts that can be used to perform various tasks. Modules are the building blocks of Ansible playbooks.
In the example, the
apt
andservice
modules are used for package management and service control, respectively.
Idempotence:
- Ansible is idempotent, meaning that running a playbook multiple times will not change the result after the first run if the system is in the desired state.
Variables:
- You can use variables in playbooks to make them more flexible and reusable. Variables can be defined at various levels, including at the playbook, play, or task level.
Handlers:
- Handlers are special tasks that are only executed when notified by other tasks. They are often used to restart services or perform other actions after changes.
Conditionals:
- Ansible playbooks support conditionals, allowing you to execute tasks based on certain conditions.
Roles:
- Roles are a way to organize and reuse Ansible content. They allow you to group related tasks, files, and variables together.
Running Playbooks:
- Playbooks can be executed using the
ansible-playbook
command.
- Playbooks can be executed using the
ansible-playbook my_playbook.yml
- You can also use the
-v
(verbose) option for more detailed output.
Ansible playbooks are powerful tools for automating complex tasks and configurations across multiple servers. They provide a structured and readable way to express automation logic.
Create directory to write playbooks
Play book to
ansible-playbook <playbook_name>
ansible-playbook date_play.yml
ansible-playbook -v date_play.yml
-v gives detailed output
Added uptime
ansible-playbook -v date_play.yml
Install Nginx using Ansible Playbook from Master node to Slave Nodes
Here, Installing Nginx on servers mentioned in only "servers" group
After Nginx Installed edit code for Start Nginx also
ansible-playbook install_nginx_play.yml
Hosting static website on server mention in group "prod"
Nginx static page is served at /var/www/html
Ansible playbook that installs Nginx and copies your static website files to the specified directory.
Get any sample index.html file from internet
Yayyyyy!! Hosted static website on Server
Conclusion:
We've taken a step-by-step journey through setting up a basic infrastructure on AWS using Ansible. We created instances, installed Ansible, and configured connections between nodes. Troubleshooting hiccups, we learned to use Ansible commands and organized servers into groups for more targeted updates.
The introduction of Ansible Playbooks brought automation into play. We executed tasks like displaying system information, updating packages, and installing Nginx. The major part was hosting a static website on specified servers, showcasing the practical magic of Ansible in simplifying complex tasks. This experience highlights the simplicity and power of Ansible in automating complex tasks, making server management.
Day 13, Day 14 are posted on my LinkedIn
π Thank you for taking the time to explore this blog!π I hope you found the information both helpful and insightful.β¨
π Enjoy your learning journey, and don't hesitate to reach out if you have any feedback. π€ Happy exploring!!!!!