In this video, you'll get a quick introduction to setting up Ansible for configuration management.
Let's first talk about the requirements. There is a control host. That is where you are going
to push the desired state to the managed machines. And a control host, I would say you send to
a stream or similar, but the control host can be anything nowadays. Ansible control
hosts run successfully on any Linux distributions, on Windows, on Mac OS. But as Ansible is owned
by Red Hat, integration is probably easier on Red Hat based distributions. Also on the
control host, you need to set up hostname resolving for all managed nodes, unless you
want to contact these nodes by IP address. And you generate SSH keys and copy over to
the managed host to make sure that your current Ansible user on the control host can connect
to the managed host without any issues. And next you install the Ansible software. You
create an inventory file, and that's about it. Then on the managed host, you ensure that
Python is installed. That shouldn't be a big deal nowadays, as Python is a part of the
minimal Linux configuration. All Linux distributions, except for the very minimal cloud-based distributions,
have Python on board. You need to enable SSH access, and you need to make sure that you
have a user with sudo privileges. If you want to make it easier, and that's what we are
going to do here, that should be a password-less sudo privileges. But don't worry, in order
to use Ansible, you can do it a secure way as well and make sure that passwords are used
in order to push the configurations. So how do we set up Ansible? Well, on the managed
host, like Ubuntu for instance, you need to make sure that you install OpenSSH server.
And then on the control host, you install the Ansible core package. You make sure that
you have your hostname resolution set up. You generate some SSH keys, copy that key
for the same user to the managed machine, and then you update your inventory. And then
it's time for the first command, which could be ansible ubuntu minus mping, which is using
the ping module, which is an Ansible module. Ansible is doing its work by using modules.
Minus i inventory is using the inventory. Minus your student is using your student user,
which should be the current user in this example. And minus uppercase K will prompt for the
sudo password. Let me show you. So here I have the Ubuntu host that I want to use,
and I need the IP address. It's giving me lots of information. But here is the information
that is relevant. The IP address ends in 146. Next, I need to make sure that I do my sudo
apt install OpenSSH-server. This is an Ubuntu desktop, and Ubuntu desktop does not have
OpenSSH-server by default. So make sure you install it. Fortunately, on server editions,
you normally do have SSH installed. And now I'm going to set up my control host. In order
to set up the control host, I need to be student user. I don't have to be the root user. First,
I'm installing the Ansible package using sudo dnf install minus y ansible-core. As a quick
check, I'm using Ansible minus minus version, which is showing that this has installed Ansible
version 2.16.14. So Ansible is installed. Next, I'm going to use sudo sh-c echo 192.168.29.146.
I'm calling it ubuntu.example.com, short name Ubuntu. And I'm redirecting this to etc hosts.
Let's use double quotes. No need to use single quotes. Now, what is this command doing? Well,
we are running sudo. But the problem is that there's a redirect, and the redirect causes
confusion. And that is why I'm using sudo on the command sh-c, where sh-c executes the entire
command, including the redirect. If ever you want to use composed commands that have a redirect or
a pipe in them in sudo, it is wise to use sudo sh-c in the entire command between double quotes.
So as the result, I should be able to ping Ubuntu. So this proves that I can reach the
Ubuntu machine by its name. Now I'm going to generate SSH keys. For this demo, I want to
keep it simple. So I'm using my current sudo enabled user account, which is a student user.
Ubuntu has a student user as well. So I'm using ssh-keygen to generate a public private key pair,
and then ssh copy ID to Ubuntu, which is copying my public key over to Ubuntu.
I need to enter the password once. And now we have passwordless authentication
on the remote host. A quick check when I use SSH Ubuntu. I'm on the Ubuntu machine
using exit, and I'm back. Okay, now I need to create a so-called inventory. Echo Ubuntu
greater than, greater than inventory. The inventory file for Ansible identifies all
the managed assets. So it's a pretty important file. Without an inventory file, Ansible only
knows localhost. So Ansible doesn't use hostname resolution. When you are going to use an Ansible
command, Ansible uses inventory to find more information about managed hosts. And now I can
use a so-called Ansible ad hoc command. Doing that using Ansible Ubuntu minus m ping minus
i inventory. I'm using the ping module. The ping module, in fact, is a Python script that can be
used to ping assets in Ansible. Minus i inventory is telling Ansible to use the inventory file I
just created in my current directory. Minus u student is using the student user, which is my
current user account. And not really necessary here, but let me show you anyway. Minus uppercase
k will prompt for the sudo password at the other side. So there we go. Here is the become password.
I'm entering it. And here we see the result of the Ansible ping module. So what does that mean?
Congratulations, you just successfully ran your first Ansible command.