Hello everyone, welcome back to the channel
Today, I will show you how to install Ansible on Ubuntu 20.04
Later, I will also cover how Ansible is used to automate IT infrastructure with the demo
Let's understand what Ansible is all about. Ansible is an open source tool for automating IT tasks
It uses plain English playbooks to manage your infrastructure, making it easy to deploy
applications, configure systems, and orchestrate complex workflows. There are several reasons why Ansible is a popular choice for automation
The first one is simple and easy to learn. Playbooks are written in Yemen, a human-readable language, making Ansible easy to pick up even
for beginners. Agent-less design. Unlike some tools, Ansible does not require any agents to be installed on many systems
It uses SSH for secure communication. Powerful and flexible. Ansible can handle a wide range of tasks from simple configuration management to complex deployments
For the demonstration, I am using the following lab with 3 VMs
VM1 with Ubuntu 20.04. I am calling this VM as Ansible Control Node because I will be installing Ansible on this node
VM2 running on Fedora 40. I am calling it as Ansible Manage Host 1
VM3 again running on Ubuntu 20.04. I am calling it as Ansible Manage Host 2
I have already connected to these VMs via SSH session. This is my first VM
This is my second VM that is Manage Host 1 and this is third VM which is Manage Host 2
On this first VM, I will be installing Ansible. But before that, we have to configure a common user on all the 3 VMs that it can run admin
commands without prompting sudo password. I have already created a sysops user on all the 3 VMs. Ok
Next run this command so that this user can execute all the admin commands without prompting
sudo password. With the interest of time, I have already prepared the set of commands and steps that
I will be referring it in this demo. I will share these steps in the description of the video for your reference
Run this command on all the nodes
Next add Ansible pp repository in order to install latest version of Ansible
Run the following set of commands. sudo apt update
In my case, this package is already installed. Now finally add the repository using the command add apt repository
Now we are all set to install Ansible. Run sudo apt install ansible-y hit enter
Once the installation is complete, verify the Ansible version. Run ansible-version
So we are running an Ansible core of version 2.16.7 and the config file is located under
slash etc ansible-ansible.cfg. Next is to test Ansible installation. But before that we need to configure passwordless SSH authentication for SysOps user among all
the three VMs. Generate SSH keys for SysOps user from the control node and share it between VM1 and VM2
Run SSH keygen trs8 hit enter
This is the public key for SysOps user and this one is the private key
Now share this public key to the VM1 and VM2 using SSH copy id command
Similarly run for the second VM
This output confirms that keys has been added for both the VMs
In order to test Ansible installation, we must create an ansible.cfg and the inventory file
In this demo, I am creating these files under SysOps home directory
But the default location is ansible.cfg as we have already seen in the previous command
These files are generally created based on the projects. So different projects will have different ansible.cfg and inventory files
Let's create our project. We call it ansible-demo
Go to this folder. Run ansible-config space init command to generate the ansible.cfg file
Next edit this ansible.cfg file. Add the parameters under default section and privilege acceleration section
Privilege acceleration here specifies the path of our inventory file that will be used
by ansible. Remote user is equal to sysops. We will be using a sysops user while connecting to the remote systems
In our case, it would be VM2 and VM3. Host key checking is equal to false
Next search acceleration
Add these four parameters under privilege acceleration section. VM2 means it will allow sysops users to become a root user using sudo
And while becoming a root user, it will not prompt for the password
Save the changes and exit the file. Now create the inventory file
Give the name as dev
One is release. Under dev group, add the VM2 IP
Under release, add VM3 IP address. Save and exit the file
Now run ansible space hyphen hyphen version command to verify whether ansible is picking
our ansible.cfg and the inventory file we have just created. Make sure you have to run this command from the ansible-demo folder
Inventory file is pointing to our ansible.cfg file. Let's verify the connectivity of managed nodes from the ansible control node using adobe command
run ansible ping
This command will perform a ping pong test towards the VM2 and VM3
The output confirms that the ansible VM or the ansible control node is able to reach
the VM2 and VM3 successfully using the sysops user. Let's create a sample playbook that will install the packages on dev and release servers
In this playbook, we have two plays. One is for the release, another one is for dev
For release, we are installing lamp and for dev, we are installing nginx
Save and close the file. Before actually applying this playbook, let's do a dry run of this playbook
run ansible playbook new entry packages hyphen hyphen check
This output confirms that there is no syntax error in the playbook
There is no connectivity issue. So we are safe to apply this playbook
hyphen hyphen check in the previous command and then hit enter
This output confirms that the playbook has been executed successfully on the dev as well
as on the release servers. We can verify this by running an adobe commands
This command will run against the dev servers and it will execute rpm hyphen qa and will
grab nginx. If nginx is installed, then we will get the output something like this
This confirms the nginx package is already installed on the dev servers
Similarly, run the same command for release
In the release, we have installed lamp stack. So we are using dpkg hyphen l because in release we have ubuntu and in ubuntu we have dpkg utility
and we are graphing this php, mariadb and the apache too. Hit enter. Great
The output confirms that apache 2, mariadb, php, they are installed on this release server
That's all from this video tutorial. I hope you have found it useful and informative
Please like the video and subscribe to our channel for more technical tutorials
Thank you. Bye.