Author: Pulkit Bindal
Automating infrastructure management requires writing YAML scripts for Ansible playbooks and inventories. Ansible maintains a list of hosts or nodes, along with the variables and groups that will arrange them, in the inventory file. The tasks and directives that Ansible executes on the inventory hosts are defined in playbooks, which are YAML files. Variables, loops, and conditionals can be included to support sophisticated configuration management. IT organizations can streamline deployments, configuration, and maintenance by using carefully prepared inventory and playbook YAML scripts, which will reduce errors and boost productivity.
In this blog, we will explore how to install Ansible, establish SSH connections, and create inventory and playbooks. Later, we will demonstrate how to print some dummy messages or install a package on the Azure virtual machine host using Ansible playbook.
For the successful installation of Ansible and its components, we will be using the following prerequisites:
1. To do this practically we need two VMs. For Instance:
- Ubuntu Terminal: This is my controller node or the master node, currently installed on Windows using Windows Subsystem for Linux. The latest pip and python3 must be installed.
- Azure Virtual Machine: This is my managed node or the slave node, currently hosted on some cloud servers. Here, changes will be made from the master node. The installation of the latest packages will work here, and you can also write some Hello World messages.
2. Next, you will definitely need a basic knowledge of Linux and how to work on the Linux Terminal. Lastly, both your VMs should be connected using a bridge adapter. This setup will allow both VMs to reach the Internet.
Installation of Ubuntu Terminal via Microsoft Store
Here are the steps to install Ubuntu in Windows using the Microsoft Store:
- Open the MS Store application in Windows.
- Search for “Ubuntu” in the search bar and select the “Ubuntu” app from the search results.
- Go ahead and start the installation process.
- Wait for the download and installation to complete.
- After installation is done. Start the application.
- The first time you launch the app, it will take a few minutes to set up and configure.
- Follow the on-screen instructions to create a new user account and password for Ubuntu.
- After completing the setup process, you will be able to run Ubuntu commands and use Ubuntu applications directly from your Windows computer.
Note: All the setup will be done in the Ubuntu terminal, which is present in Windows using WSL. The effects of the playbook script will be observed in the Azure Virtual Machine, the host.
By utilizing the Microsoft Store, you have successfully installed Ubuntu on Windows.
Validate Path to Python
Find the Python interpreter you want to use to run Ansible and note its location. This Python is referred to as python3 in the instructions that follow. For instance, specify python3.9 rather than python3 if you want to use the Python at /usr/bin/python3.9 when installing Ansible.
Ensuring pip is available
Let’s verify the pip :
Command: python3 -m pip -V
pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)
Install pip under your preferred Python interpreter if you encounter an error such as ‘No module named pip’ before continuing. This could entail downloading the most recent version of pip from the Python Packaging Authority or adding a new OS package (such as python3-pip) by performing the following steps:
Command: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Command: python3 get-pip.py –user
Now install the Ansible package the ansible named user:
Command: python3 -m pip install –user ansible
Since my requirements are already satisfied, it is saying ‘Requirement already satisfied.’
For a fresh environment, it will start installing for sure.
To upgrade the existing version, run the following command:
Command: python3 -m pip install –upgrade –user ansible
Confirming your installation
Let’s check the Ansible version:
Command: ansible –version
To check the version of the Ansible package that has been installed:
Command: python3 -m pip show ansible
How to create your inventory?
Ansible automates tasks on controlled nodes or ‘hosts’ in your infrastructure using a list or collection of lists known as an inventory. Though most Ansible users create inventory files, host names can also be supplied at the command line. The managed nodes you automate are included in your inventory, and groups allow you to perform automation operations on several hosts simultaneously. Once you set up your inventory, you can use patterns to specify which hosts or groups you want Ansible to run against.
The rudimentary inventory can consist of a combination of hosts or groups. This file’s default location is /etc/ansible/hosts. By utilizing the -i option, you can use the command line to provide an alternative inventory file.
Inventory Basics Components: Formats, Hosts, and Groups
Depending on the inventory plugins you have, you can produce your inventory file in one of several different forms. As you might notice, the popular file formats are YAML or INI. Here is an example of a live hosts file located at /etc/ansible/hosts:
184.108.40.206 ansible_user=azureuser ansible_pass=Pulkit12345@
Because Ansible by default utilizes this folder to store configuration files, as I will demonstrate shortly, the folder “/etc/ansible” is used here.
The next step is to include the username (azureuser) and password (Pulkit12345@) for my managed node in the “hosts” file since, as you may have already seen, Ansible requires the SSH protocol to execute anything on the Managed Node. Here, I’ve used the terms “ansible_user” and “ansible_password” explicitly to keep things easy.
Setting up Configuration file:
Our hosts file has been correctly set up, but how can we find out where our inventory information is? Therefore, we must specify which file contains our inventory in the Ansible configuration file.
In order to do that, you must paste the YAML code below into a file named “ansible.cfg” located in the “/etc/ansible” folder.
inventory = /etc/ansible/hosts
host_key_checking = False
As you can see, I entered the inventory location in the exact same file that we just produced.
Next, “host_key_checking” is set to false because, if you are familiar with SSH, you will know that, in order to connect to a remote system, you must type “yes” immediately following the “ssh” command. You may think of this as host key checking in the simplest terms.
We set “host_key_checking” to false because Ansible is an automation program, and there isn’t a physical person to type “yes” while performing SSH.
Finally, the Ansible setup is complete. Now, it’s time to check a few items. By following the instructions listed below, you can determine whether your environment is prepared or not by examining the results.
Command: ansible all –list-hosts
Command: ansible all -m ping
Twist: When a user needs to automate a procedure that involves SSH connections and requires password authentication, the need for the sshpass utility becomes apparent. Let’s focus on the SSH Connections.
For instance, let’s say you need to execute a script that automates backups and transfers the backup files to a distant server using SSH. In this situation, you can use sshpass to supply the password to the script’s SSH command, enabling the script to run automatically.
Solution: You need to add your public SSH key (Control Node – Ubuntu Terminal) to the authorized_keys file on each remote system (Managed Node – Azure Virtual Machine).
Test the SSH connections, for example:
Command: ssh email@example.com
After hitting this command, you will be redirected to the Azure VM, marking the successful initiation of your connections. This is a great start to your setup.
Success and Congratulation! We have successfully set up our Ansible environment on our Control Node.
Create a file called “playbook.yaml” in the default location, which is /etc/ansible/playbook.yaml. Then, paste the below YAML script into the file.
- hosts: azurevm tasks: - name: Ping my hosts ansible.builtin.ping: - name: Print message ansible.builtin.debug: msg: Hello from Pulkit`s Ubuntu Terminal.
As you can see, I first defined the hosts group name as “azurevm,” indicating that I wanted to run this playbook on the host of my Azure VM.
The “tasks” keyword is used next in the playbook to indicate that the tasks listed under “tasks” are the ones I wish to complete.
Before using this playbook, let’s examine its syntax. Run the command listed below, which is displayed in the screenshot, in the folder where you saved the “playbook.yaml” file.
Command: ansible-playbook playbook.yaml –syntax-check
Command: ansible-playbook playbook.yaml
Since I don’t have any issues, as you can see in the screenshot, the playbook file name is displayed.
Additionally, we have successfully pinged and remotely published a “Hello World” message using a playbook file against managed nodes, such as Azure Virtual Machines.
Congratulations! We have successfully learned in-depth how to install Ansible, create inventory, and use playbooks in Ansible.