Table of contents
- What is Ansible?
- How Ansible Works?
- Ansible Architecture: Nodes and Modules
- For a more comprehensive version of this blog post, please refer to the previous entry where you can find a thorough and hands-on rephrasing of the content:
- Create 3 Instances on AWS EC2 with the following names:
- Installation of Ansible on AWS EC2 (Master Node)
- Create SSH key on Master Node
- What is a Hosts file?
- Where is the Hosts file located?
- Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.
- What is the Ansible Playbook?
- How to create an Ansible Playbook?
- Create a playbook to install Nginx
- Deploy a sample webpage using the Ansible playbook.
- <mark>Node_1 Output:</mark>
- <mark>Node_2 Output:</mark>
- <mark>Node_3 Output:</mark>
What is Ansible?
Ansible is a software tool that provides simple but
powerful automation
for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis.Ansible doesn't depend on agent software and has
no additional security infrastructure
, so it's easy to deploy.
How Ansible Works?
Ansible works by
connecting to nodes
(clients, servers, or whatever you'reconfiguring
) on a network, and then sending a small program called aAnsible module
to that node. Ansible executes these modules over SSH and removes them when finished.SSH keys are the most common way to provide access, but other forms
authentication
are also supported.
Ansible Architecture: Nodes and Modules
Ansible's Architecture is based on the concept of a control node and a managed node. The platform is executed from the control node where a user runs the
ansible-playbook
command. There must be at least onecontrol node
; a backup control node can also exist. The devices being automated and managed by the control node are known as managed nodes.Ansible automates
Linux
andWindows
by connecting to managed nodes and pushing out small programs calledAnsible modules
. Ansible executes thesemodules
, which are the resource models of the desired system state, over Secure Socket Shell (SSH) by default andremoves
them when finished.Ansible
modules are written in Python and can be written in any language.Ansible
modules are reusable, standalone scripts that can be used by the Ansible API, Ansible Playbooks, or Ansible Galaxy.
For a more comprehensive version of this blog post, please refer to the previous entry where you can find a thorough and hands-on rephrasing of the content:
Create 3 Instances on AWS EC2
with the following names:
Ansible_Master_Server
Node_1
Node_2
Installation of Ansible on AWS EC2 (Master Node)
#!/bin/bash
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
sudo chmod 777 install.sh
sudo ./install.sh
ansible --version
Create SSH key on Master Node
cd ~/.ssh
ssh-keygen
What is a Hosts file?
- In the context of Ansible, a
host file
(also known as an inventory file) isconfiguration file
used to define and organize the list of target hosts that Ansible should manage.
Where is the Hosts file located?
Ansible uses this file to map target hosts to managed nodes. The host file is usually located in
/etc/ansible/hosts
.So, open the host file and add the IP addresses of the Nodes:
sudo vim /etc/ansible/hosts
[servers]
Node_1 ansible_host= <Public IP-Adddress of Node-1>
Node_2 ansible_host= <Public IP-Adddress of Node-2>
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/id_rsa
Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.
# Open Node_1
cd /home/ubuntu/.ssh
vim authorized_keys
# Open Node_2
cd /home/ubuntu/.ssh
vim authorized_keys
What is the Ansible Playbook?
Ansible playbooks run
multiple tasks
,assign roles
, anddefine configurations
,deployment steps
, andvariables
. If you’re using multiple servers, Ansible playbooks organize the steps between theassembled machines
orservers
and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.It allows you to describe the desired state of your
systems
andapplications
in a declarative manner, and Ansible takes care of executing thenecessary tasks
to achieve that state.Ansible playbooks are written in YAML. YAML is a
human-readable data serialization language
. It is commonly used for configuration files and in applications where data is beingstored
ortransmitted
.YAML stands for YAML Ain't Markup Language. It is a human-readable data serialization language and is commonly used for configuration files, but it can be used in many applications where data is being stored or transmitted.
How to create an Ansible Playbook?
An Ansible playbook is a
structured
andhuman-readable document
written in YAML (YAML Ain't Markup Language) that defines aset of tasks
and configurations to be performed on remote servers. Playbooks are the heart of Ansible automation and allow you to define the desired state of your systems and applications.YAML Syntax:
Playbooks
are written in YAML format, which is ahuman-readable markup language
. YAML uses indentation and colons to structuredata hierarchically
. Indentation is crucial in YAML to define thenesting levels of data
.Play: A
playbook
starts with alist of plays
. Each play targets a specific group of hosts and defines a set of tasks to be executed on those hosts.name: A user-friendly name for the play.
hosts: The group(s) of hosts this play will target. Host groups are defined in the inventory file.
become: Indicates whether privilege escalation is required to perform tasks (using sudo).
tasks: List of tasks to be executed in this play.
For Example;
---
- name: My First Playbook
hosts: web_servers
become: yes
tasks:
# Tasks will be defined here
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
name: A description of the task.
module: The Ansible module to use for the task (e.g., apt, yum, file, copy, etc.).
module_arguments: Arguments specific to the module being used.
Create a playbook to install Nginx
- Installing Nginx using Playbook:
-
name: Installing Nginx using Playbook
hosts: all
become: yes
tasks:
- name: Install Nginx
apt:
command: nginx -y
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- Output of the above playbook:
- Checking the status of Nginx on one of the Node_server
service nginx status
nginx -v
Deploy a sample webpage using the Ansible playbook.
cd playbook
# Put any index.html file in the playbook folder
sudo vim index.html
- Modify the playbook as follows:
-
name: Installing Nginx using Playbook
hosts: all
become: yes
tasks:
- name: Install Nginx
apt:
command: nginx -y
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- name: Creating Webpage
copy:
src: index.html
dest: /var/www/html/index.html
- Output of the above playbook:
- Now we will use the public IP address of the Node server to access the webpage.
Node_1 Output:
Node_2 Output:
Node_3 Output:
Thus we have successfully deployed a sample webpage using the Ansible playbook.