Lecturer: Hey folks.
In this lecture we will learn how to install, configure,
and manage Ansible in Linux.
Let's start with what is Ansible.
Now, according to Wikipedia, Ansible is a suite
of software tools that enable infrastructure as code.
It is an open source tool
and the suite includes software provisioning,
configuration management,
and application deployment functionality.
Okay, that's about Wikipedia.
Now let's put it in simpler words so you we all understand.
Ansible is an automation tool that simplifies IT tasks
like software deployment, configuration management,
and server provisioning.
So when you install, download,
install software on your server,
Ansible will help you automate that
and when you need to configure that software, it comes
to your rescue and of course when you need
to install servers
or build new servers as well as the software.
Now I'm including this topic
because every Linux system administrator
or engineer should automate repetitive daily annoying
tasks and this tool is one of the most efficient
and of course my favorite
to use when it comes to automation.
Okay, now please note the Ansible is agentless, meaning
that it does not need any type of extra software
that you need to download on your managed machine.
It uses easy to read YAML code for writing playbooks.
YAML is an easy to read code format
for storing data and configuration files.
It uses key value pair separated by colons indentation
for nested data and dashes or hyphen for list.
Now Playbook in Ansible are files written in YAML code
that contains a list of tasks to automate IT processes.
They describe the action to take on managed systems such
as installing software or configuring settings.
Let's understand Ansible with the help of an example.
Okay, let's suppose you are a system administrator
in a company, let's say ABC company
and your task is to manage a group of a 100 servers
and all those servers need the same software to install.
Instead of logging into each server one by one,
you write a playbook in Ansible server
where your main Ansible is running.
This playbook lists the steps like install the software
and set this type of configuration
and then start the service or whatnot.
When you run the playbook,
then Ansible automatically connects
to all those 100 servers
and does the work for you
while saving you company's time, your time as well.
It ensures that everything must be done exactly the same way
on each server.
Wow, that's beautiful.
So you don't have to log into every server
and install that software one by one by one.
You just tell your Ansible server, go ahead,
here's your software installed exactly on all these servers
which will be in the inventory, which we'll discuss in a bit
and it will do it for you.
Wow.
Alright.
Here you can see a simple example of a YAML playbook
where I want to install the Vim editor on the local machine.
Now, Vim editor is just a software that I picked
just for this example, you could pick any software
of your choice.
So learning from simple code is the best way.
It will help you write a YAML playbook file
when using Ansible.
Let's explain each line of it.
In YAML comments are marked with a hash symbol
and just like if you're writing a Bash shell script
and the document begins with a three hyphens
or dashes.
Lists or sequences
are created using a dash before each item.
It is useful when you have multiple items
or steps to execute in order.
In the give an example,
host is a key in the list specifying the target machine
and the become a key indicates whether tasks
should be executed with elevated privileges such as admin
or pseudo.
Proper indentation, which spaces defines the hierarchy
or nesting of items in YAML.
So indentation shows relationship
with more indentation indicating sub-levels.
Sections are created by placing a column
after a key followed by indented items
or setting under that section,
it is helpful in organizing
and grouping related tasks clearly.
So here task is a section
and inside it another subsection is defined with name DNF.
That specifies the package manager module
where the name of Vim indicating the software
to be installed and the state present
is another subsection that ensures the Vim tax editor
is installed on the system.
When this YAML file runs
or executes, Ansible connects
to the specified target machine, local host for example,
and with elevated privileges it installed
a Vim text editor using the DNF package manager
ensuring the software is present on the system.
Now if you are installing on multiple system,
then you would have to create an inventory file
and specify all the host names
or IP address off the remote machines in that file.
And Ansible will would know to connect to all those servers
and install all those packages for you.
Alright, now let's quickly go over its history.
In 2012, Ansible was created by Michael DeHaan
as a simple automation tool.
In 2013, Ansible was officially released
as an open source project
and then in 2015 Ansible was acquired by Red Hat.
All right. Then comes 2001 Ansible Automation Platform 2
was released.
That was offering enhanced features and scalability.
Now as of today, Ansible continues
to be a widely used automation tool with ongoing updates
and enhancement under Red Hat Management.
Let's get back to the main point.
From this point, I'll first go through the steps to install,
configure, and manage.
So starting with step one,
I will check whether Ansible is installed
or not on our system.
If it's not, I will install it using what?
The DNF package manager.
After the installation, I will verify
by checking its version.
Okay? Then step two,
I will edit the main Ansible configuration file host
located in Etsy Ansible directory.
In this file I will define the web servers group
and specify that Ansible should connect
to local host is using a local connection.
Local host refers to the current machine
where Ansible is installed.
In step three, I will verify the configuration settings
by pinging the local server using Ansible command.
In step four, I will use Ansible
to automate task on multiple machines.
Start by generating SSH key pair
to connect your current machine with another one machine
that we already have and that is CentOs server.
This will allow you to install the Apache server
on both machine simultaneously demonstrating
how Ansible can manage
and automate tasks across different systems with ease.
Then copy the SSH key to the remote server,
which is in our case is the CentOS server machine.
And then test the connection.
In step five, edit the Ansible host file again
and add the CentOs server with its IP and user details.
Then test the connection to the server
and create an Ansible playbook
to install the Apache HTP server
and then run this playbook
to install Apache server on both machines simultaneously.
Okay, then finally step six,
verify the Apache HTP server installation
is on both machines that it has been installed or not.
Okay,
so before any further delay,
let's move on to actual practice
and run and go through all these steps.
I already have my putty session open here.
First you always have to do is check,
you are logged in as yourself.
Who am I? I'm root.
Which directory I'm in, I'm in root,
which host name I'm on?
My first Linux VM.
IP address.
Let's check that too.
192.168.100.167.
Perfect.
We are on the right machine.
Let's check if we have the package
and for that run RPM dash QA command
and you can use Grip for whatever package you're running.
In our case we'll do Ansible of course. Okay.
As you could see it's not there
and now I will follow the detailed procedure
that I have documented earlier.
I'll open it up on the side
so that we all can follow together with these steps one
by one.
All these steps are clearly highlighted
and as you are going through with me,
I will also encourage you to open this document,
and follow along.
Alright, let's start with step one, install
and verify Ansible.
Let's start by installing Ansible
and type DNF space install Ansible and press Enter.
The installation will begin
and you will see that it's ready
to install 11 packages including Ansible Core
and Python 3.
Okay, wait a second.
If you are thinking,
"Hey Imran, didn't you say that Ansible is agentless,
you don't have to install anything?"
Well let me clarify that.
You would have to install the main Ansible server package
where Ansible will run on your main server.
Now, for the remote server
where you want the execution to happen
or whatever the task that you want
to perform on the remote server, you do not need
to install anything on the remote server.
That's why I meant agentless where you are wanting
to manage a 100 server,
you do not have to install anything related
to Ansible on those 100 servers.
Okay, so I believe that is clarified. Let's move on.
Okay, so the packages we have is Ansible Core,
and Python 3.
These are essential because Ansible core powers
the automation tasks while Python 3
is the language Ansible uses to run its modules.
Let's press y To continue the installation,
press y again to proceed.
The installation is now in progress
and verifying the components.
Once it's done, the installation will be completed.
All right, the installation is completed. Nice.
Let's move on and verify the installation
by checking the Ansible version.
Simply run the command Ansible space dash dash version.
Okay. You'll see the version here. Beautiful.
Keep in mind that this version might differ
when you practice,
so don't worry if you see a different version.
Let's move on to step two, configure Ansible host file.
Let's get started by editing
the main Ansible configuration file,
host found in the Etsy Ansible directory.
We will use the absolute path
to make sure we are opening the right file.
Inside the Ansible configuration file,
you'll find various settings that control
how Ansible operates.
Most of these settings are commented out,
meaning they are optional and can be activated
based on your preferences, you have the flexibility
to enable only those settings that suit your specific needs,
allowing you to customize Ansible
to work exactly how you want.
If you want to learn more about these settings,
I recommend you to check out docs.ansible.com.
Alright, for now, let's get the details
and go straight to the important part.
The web server block, where the real action begins.
In this section, you define a group of servers you want
to manage with Ansible.
Let's uncommon this section
and add this current machine referred as a local host.
By typing local host Ansible_connection equal local.
You are telling Ansible to include our local machine
in this group and connect to it directly without using SSH,
making it easier to manage task on the local system.
Here you don't need to use a host name
or IP address for the local machine
because Ansible already understand local hosts
as a special identifier
for the machine you are currently on.
Let's save it and quit.
All right, move on to step three,
verify the configuration settings.
Let's test the connection between Ansible and the host.
Define in the web servers group
in your Ansible configuration file
that is also known as inventory file.
Type Ansible space M, M is assured
or an option for module
and space ping and ping is the module we are using
and who we are pinging.
We are pinging the server name, web serve and press Enter.
You've done it perfect. It shows the ping pong response.
This means Ansible is successfully connected
to your current machine, named My First LinuxVM.
While it only confirmed the connection nothing has changed
it, you are now ready to start executing tasks.
Let's move on to step four, generate SSH key pair.
Let's automate task on multiple machines
by generating SSH key pair
to connect multiple machines through Ansible.
So here I have another CentOs machine running.
Let's verify it. Who am I? I am myself.
I'm in my home directory.
Host name is CentOs server
and the IP address is 192.168.100.169.
Okay, perfect. Let's go back to the party.
Session again.
Let's generate SSH key here for a CentOS server machine
by typing SSH-keygen space minus T,
space RSA,
space minus B,
space 2048 and hit Enter.
So it will create a public
and private key using the RSA algorithm with a key length
of 248 bits.
This key pair is used for secure SSH authentication
between machines, in my case,
between my first Linux VM and CentOS server machine.
Now simply press Enter at each prompt
and your SSH key will be successfully created.
Next is time to connect to your CentOS server machine,
type SSH copy dash ID command followed
by your username for CentOs server machine
and in my case it's Iafzal then at and the IP address.
In my case one 192.168.100.169 and hit Enter.
Let's type yes to add the key and then enter the password
and here you go.
You are not successfully connected
to your CentOs server machine from your current machine.
Let's test the SSH connection by typing SSH followed
by CentOs server machines, username and its IP address.
Perfect. We have successfully logged in without password
because we already have SSH key allowing passwordless login.
Alright, let's verify again
to ensure it's the correct machine.
And all the information matches, so it's the right machine.
Let's exit from it
and return to the machine where Ansible is installed.
Let's verify this machine again
and it's all good.
Moving on to step five, create the Ansible playbook
to install Apache server.
Now let's edit the Ansible host file.
Again, go to the web server section
and after the line that is for our current machine,
add the necessary details for the new server.
Let's add this line.
So here we define host as CentOs server, a root web server
with the Ansible underscore host parameter pointing
to its actual IP address and that is ending with dot 169.
The Ansible underscore user parameter specifies
that the Iafzal have a user account will be used to connect.
This setup ensures
that Ansible can seamlessly manage CentOs server using
the correct IP address and user credentials.
Okay, now let's go ahead and save and quit out of the file.
Now it is time to verify the configuration settings.
You can use either the web server option or all.
Both will work the same.
Now you can see that both servers local
and remote are successfully connected.
Now let's move on.
Before we install Apache through Ansible,
let's first check if Apache is already installed
on both machines.
Nope, it's not installed on either of the machines.
Let's automate the installation of Apache server
by creating an Ansible playbook using YAML code.
First create a new file named Install Ansible.YAML.
The dot YAML extension is essential
because Ansible playbooks are written in YAML format
and it will recognize it.
Now let's make it easy.
Just copy the configuration file from the document
and paste it in this file.
All right, so this YAML code is used
to install the Apache web server HTTPD on two machines.
My first LinuxVM
and the other one is CentOS server,
which are grouped under the web servers in the inventory.
The playbooks uses elevated pseudo privileges
to run the task as the root user.
It then ensures that the HTTPD package installed
and present on both machines using the DNF package manager.
All right, let's save it and quit. All right.
Now it's time to kick off the Apache installation
by running the playbook by typing Ansible-Playbook.
Install httpd.yaml.
Space dash dash ask dash become dash pass command
and press Enter.
This executes an Ansible playbook name
as install underscore Ansible.YAML,
which installs Apache on these two servers.
Here the dash dash ask dash become dash pass option
prompts you to enter the pseudo password
for the remote machine where Ansible will perform tasks.
Now, Ansible prompts you to enter the pseudo password
for the remote machines, type the password
and press Enter.
Ansible connects via SSH to each
of the remote machines listed in the web servers group
in your inventory file.
It then executes
the task defined in the install_httpd.yaml playbook.
In this case, it checks if the httpd package is installed
and if not it install the package.
Now Ansible displays the results for each machine.
Showing whether the httpd package was already installed,
newly installed, or if any errors occurred.
Finally, Ansible summarizes the results highlighting
which tasks were successful, any changes made,
and confirming that all tasks completed without errors.
Finally, moving on to step six,
verify the Apache httpd server installation.
Let's verify whether the httpd package is installed
on both machines.
All right. Here you could see
that httpd package has been successfully installed on both.
Beautiful.
And folks, that's the end of our session on this topic.
Now it's your turn to explore,
run some Ansible playbooks on your system to see
how automation can simplify your tasks
and manage your infrastructure.
Dive in and discover the power of Ansible behind the scenes.
I'll see you in the next lecture.