Unit 4 - DO Notes
Unit 4 - DO Notes
INTRODUCTION TO ANSIBLE:
Ansible is an open source automation tool and it was written by Michael DeHaan
and acquired by Red Hat in 2015.
It is used to perform the tasks such as configuration management, application
deployment, intra-service orchestration (meaning automated configuring,
coordinating and managing of computer systems and software) and provisioning.
Ansible can perform 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.
Ansible is agentless, temporarily connecting remotely via SSH (Secure shell network
protocol) or Windows.
Advantages of Ansible:
Free: Ansible is an open source tool. Hence it can-be freely downloaded and
installed.
Simple to learn: No special coding skills are required for ansible to use. Instructions
written in Ansible are Human - readable hence to use ansible it requires no
programming
skills.
Flexible: Ansible can be used to automate a wide range of IT tasks, from simple
configuration changes to complex application deployments.
Security: Ansible is a secure tool that can be used to automate the tasks without
compromising the security of our IT infrastructure.
Scalability: Ansible is used to automate the configuration and management of large
IT infrastructure. It can be used to automate the tasks across thousands of servers
and devices.
Powerful: Ansible allows us to model high complex IT infrastructure.
Features of Ansible:
1. Configuration management:
In Ansible we can perform the configuration management using playbook file.
The ansible can perform the configuration management tasks such as -
Installing the software on servers in designated IT infrastructure.
Configuring the services
Managing users and groups
Backing up data
Updating system files
Copying the desired files at desired computer
2. Application deployment:
The application deployment feature of ansible involves using ansible to automate
the process of deploying and managing applications on target servers or
infrastructure.
The ansible playbook can be written to perform following tasks.
Configuring services
Deploying files and directories
Creating users and groups
Managing databases
Scaling applications
3. Orchestration:
Ansible orchestration is a process of automating the tasks such as deployment,
configuration and management of IT systems and applications.
Ansible orchestration can be used to automate tasks across a wide range of
platforms, including Linux, Windows and macOS.
It can also be used to automate tasks across a wide range of infrastructure,
including servers, networks and storage.
4. Cloud provisioning:
Ansible cloud provisioning is the process of automating the creation and
configuration of cloud resources, such as virtual machines, storage and
networks.
The ansible is used to perform following tasks under cloud provisioning -
Creating virtual machines
Attaching storage
Configuring networking
Deploying applications.
ANSIBLE MASTER/SLAVE CONFIGURATION:
In ansible, there are two categories of computers:
The control node and managed nodes.
Fig: Ansible architecture
Control node:
The control node is a computer on which the ansible is installed. There must be at
least one control node.
From the control node the IT infrastructure is managed.
It serves as a central point for executing the ansible commands and playbook.
The control node contains an inventory file that contains the list of slave nodes that
a
master node can control.
Managed node:
The managed nodes are basically the servers or systems that we want to manage
using
ansible.
The Ansible connects to these nodes and runs the tasks on it using the control node.
The only requirement for this interaction is that your ansible control node has login
access to the managed nodes.
The ssh keys are the most common way to provide access, but other forms of
authentication are also supported.
It is not necessary to install ansible on managed nodes.
These nodes only require ssh access and required software can be installed.
Ansible Inventory Files:
The inventory file is also called as host file.
It is a text file that contains the list of managed nodes. These nodes can be arranged
in different host groups. This can be useful for organizing your hosts and for running
tasks on specific groups of hosts.
For example, we can create a group called “webserver” that contains all of our web
servers. We can then run a playbook that only affects the web servers by targeting
the “webserver” group.
The inventory file is an essential component of ansible architecture.
In the inventory file the IP address of each node is specified.
For example -
[webserver]
192.168.1.23
web host1
[dev]
192.168.1.01
192.168.1.02
[prod]
192.168.1.111
192.168.1.112
The inventory file is used by ansible to determine which hosts to run tasks on.
When ansible runs a playbook, it will first look for the inventory file in the current
working directory. If the inventory file is not found, ansible will look for it in the
default location, which is /etc/ansible/hosts. We can modify this file to meet our
needs.
INSTALLATION PROCESS:
Ansible can run on various operating systems, including Linux, macOS and
Windows.
However, it‟s most commonly used on Linux. So, we will make use of Linux operating
system to install Linux on it.
Ansible is written in Python, so we need to have Python installed on our computer.
Prerequisites:
The prerequisites for installing ansible on control node are -
1. A Linux or Unix operating system
2. Python 3.9 or later
3. PuTTy and PuTTyGen
4. SSH access to manage the nodes
The prerequisites for installing ansible on the managed nodes are -
1. Python 2.7 or later
2. Putty to display commands
3. A user account that can connect to control node via SSH
If the ansible is to be installed using Windows control node then we need Windows
subsystem for Linux (WSL).
Creation of Account on AWS Cloud Service:
We will install Ansible on Linux machine.
For that purpose, we will not use the local Linux machine.
In-fact we will use AWS cloud, create our account over there and we will use the
Linux machine present on the cloud.
Following are the steps that demonstrate how to create a free account for using AWS
cloud services -
Step 1: Open the web page for aws.com for creating a free account. Click on Create a
Free Account.
Step 2: Enter the necessary details for creating an account such as email-id and user
name.
Click on Verify email address. It will send some verification code to your email account
which you have mentioned. Open your email, get the verification code and enter it.
Step 3: Set the password for your account.
Step 4: Then on next subsequent pop - up windows enter the personal details such as
your address, phone number, country etc.
Step 5: Then on the next screen enter debit or credit card details and PAN number.
Don‟t worry, your bank account will be secure, it wont withdraw any amount from your
account if you are using the free tier services. It is completely safe to share debit card
details.
It will send an OTP on your phone to get-verified. Enter the OTP and proceed.
Finally, on completion of sign up process following window will appear:
Thus three Linux Machine instances - One is control node and other two are the
managed nodes are created.
We can get the public ip and private ip of each instance by selecting that instance.
Following screenshot illustrates how to get the public or private ip of each node -
Now click on SSH->Auth->Credentials. Select the path of the key which we have
generated above in step 2(a).In my case the key is ansiblekey.ppk. Click Open. Then
click on the warning which appears.
The PuTTY then opens a terminal as follows -
Edit the following line to this file so that the ansible user will get all the privileges that
the root user has,
Repeat the above step for both the ManagedNodes i.e. ManagedNode1 and
ManagedNode2
Step 10: Setup ssh configuration file
We have to edit the ssh configuration file. Hence open this configuration file present at
the
ControlNode.
[root@ip-172-31-43-225 ec2-user]# vi/etc/ssh/sshd_confg
Do three tasks inside this file. For that switch in the insert mode by simply typing “i”
Task 1: Uncomment the line “PermitRootLogin yes”
Task 2: Uncomment the line “PasswordAuthentication yes”
Task 3: Comment the line “Password Authentication no”
Save the file by: wq (i.e. colon, w, q)
Do these tasks in ManagedNode1 and ManagedNode2 as well.
then restart the sshd service at all three terminals by issuing the following command
# service sshd restart
Step 11: Connect control node with managed nodes
At the ControlNode terminal issue the command,
ssh <Pri vate IP of Node>
For example,
$ ssh 172.31.42.197
Then enter the password which we have set in step.
Similarly, we can connect to ManagedNode2 by entering the command ssh along with
its
Private IP.
Open the terminal of ManagedNode2 and verify if the file named test_file1 is getting
created or not by typing the ls command.
The student dictionary has three key - value pairs - name, std and address. The
address dictionary has three key value pairs street, city and state.
Multi-line values: There are two ways to define multi - line values in YAML.
Literal style: We can define the multi - line values in YAML by using | character
at the start of value followed by newline. This is a way to represent multiline text
in a more readable format while preserving line breaks and indentation.
os: |
Windows
Linux
MacOs
The output of above representation will be -
Windows\nLinux\nMac Os\n
Folded style: Use the > character to start the value and then end the value with
a newline character (\n). This is useful for writing long paragraphs of text
without introducing line breaks. newlines are converted to spaces, but it can be
useful for preserving leading indentation. For example,
os:>
Windows
Linux
MacOs
The output will be,
Windows Linux MacOs\n
Data types: YAML supports various scalar data types, including strings, numbers,
Booleans and null values. Scalars do not require special syntax; you can simply
write them as plain values. For example -
name: Arun
age: 21
is_married: false
null_val: null
YAML is a superset of JSON: As YAML is a superset of JSON (JavaScript Object
Notation), any valid JSON document should be valid YAML.
For example
{
“os”: “Enter your favourite operating system”,
“osystems” [
“Windows”, “Linux”, “MacOs”
]
}
The above can be converted as -
“os”: “Enter your favourite operating system”,
“osystems”: [“Windows”, “Linux”, “MacOs”]
Benefits of YAML Files:
Human-readable: It is more human-readable. YAML allows you to represent complex
data structures in a human-readable format.
Simple to use: It has a simple and clean syntax. YAML is easy to learn and Simple
to read. It can express a wide variety of different native data structures.
Fast: It has fast implementations. YAML is fast to load and easy to process in
memory.
Unambiguous: It is unambiguous. YAML unambiguously specifies the data
structures of the serialized data.
Portable: It is portable across most programming languages. YAML supports
representing sequences as lists and mappings as dictionaries.
Matching with popular languages: It matches native data structures of modern
programming languages such as Python, Ruby and JavaScript.in a language-
independent manner.
Secure implementation: It has secure implementations.
ANSIBLE PLAYBOOKS:
• An ansible playbook is a set of instructions that ansible uses to automate tasks on
remote
hosts. Playbooks are written in YAML, a human-readable format.
• The playbooks are similar to an ansible to-do list that has a list of tasks.
• The extension to the playbook file is .yml or .yaml.
• The structure of playbook is as shown below
• defaults: It stores the default variable for the role. For example, the default port
number is http is 8080, then it can be stored in defaults.
• files: This folder contains the files required to transfer or deploy to the target
machines.
• handlers: It contains handlers, which may be used by some role.
• meta: It defines some data or information about the role.
• tasks: It contains the main list of tasks to be executed by the role. These tasks might
be
defined in separate files as per the functionalities.
• templates: It contains the templates which can be deployed via a role.
Demo Example:
Start EC2-instance for ControlNode, ManagedNode1 and ManagedNode2
• Copy the public IP of ControlNode, Open PuTTY and paste it
• Select the path for your ansible_key.ppk file and insert it in PuTTY at Credentials
• This will allow you to open a terminal window for ControlNode.
• Repeat these steps for MangedNode1 and ManagedNode2 to get their terminal
windows.
Step 1: In the ControlNode terminal window, firstly login as EC2-intstace, then as root
user and then as ansible user. In my case the name of my ansible user is ansible_user.
Create a directory named roles using the command.
$ mkdir roles
Then switch to the roles folder using the command cd roles. To create a ansible roles,
use ansible-galaxy command which has the templates to create it.
The syntax for the command is ansible-galaxy init <name_of_role> --offline
Following screenshot creates a role named myweb
Ansible galaxy is a galaxy website where users can share roles and to a command – line
tool for installing, creating and managing roles. To create a role we use the ansible -
galaxy init command.
Here the name of the role is myweb. You can give any name of your own choice. The -
offline attribute is used to avoid the online repository getting downloaded.
Now issue the tree command. If the tree command is not working then install it using
the
command
$ sudo yum install tree
If we issue tree command then
We have got the clean directory structure with the ansible - galaxy command. Each
directory-must contain a main.yml file, which contains the relevant content.
Step 2: Now we will create different tasks and each task will be written in separate file.
These tasks files must be specified in main.yml file of tasks folder. Hence first of all , we
write the code in main.yml files. Just locate the tasks directory and open the main.yml
file using vi editor
Step 6: Finally, we will create a master.yml file which will invoke all the tasks defined in
tasks folder. This task file must be created at the top level of the roles directory.
master.yml
--- # This is master file that calls the roles
- hosts: dev
user: ansible_user
become: yes
connection: ssh
roles:
- myweb
The master.yml file is located inside the roles folder
Now, prior to actual execution of master.yml file, we can issue the following command
to check if any syntactical error exist or not –
$ ansible-playbook master.yml –syntax-check
Then issue the actual execution command, as there are no syntactical error in our
master.yml file. The command is ansible-playbook master.yml.
Step 7: We can verify the execution of above web server by following steps -
(1) Open the EC2-instance of ManagedNode1, copy the public IP of that instance.
(2) Open the web browser such as Microsoft Edge or FireFox and issue the web address
as http://<public_ip_of_instance _host>. Your index.html page will be displayed as
output.
We can select particular host from the group of hosts. For instance, if the group of
servers is named as dev then dev [0] denotes first server in the group, dev [1] represents
second server and so on. If we denote dev [-1] then it denotes the last server.
(3) To check the uptime of all the servers can be obtained by using following
commands,
$ ansible dev -m command -a uptime
$ ansible dev -m shell -a uptime
$ ansible de -a uptime
The result can be -