How to Install Ansible

How to Install and Configure Ansible?

Automation is the most crucial intervention in the field of technology. The automation of redundant tasks in the manufacturing or business processes ensures improvement in productivity. So, different tools for automation and platforms for orchestrating the management of automated systems and processes become mandatory requirements. If you want to install Ansible, then you should know what exactly Ansible is and its various functions.

In simple terms, Ansible is a powerful, open-source automation software ideal for configuration, management, and deployment of software applications on nodes. Ansible can also be considered as a configuration management system ideal for administrators and operations systems to ensure effective control over multiple servers.

Ansible Basics Video Course

Despite the availability of many renowned configuration management systems such as Chef and Puppet, Ansible is a trusted alternative because of the Ansible best practices and smaller overheads. Professionals nowadays are more encouraged to learn Ansible and recognize their skills with an Ansible certification. The following discussion would provide insights into the processes to install and configure Ansible. Furthermore, the report would also reflect on the working and uses of Ansible for supporting the discussion further.

Understand the Working of Ansible

The first concern in this discussion should focus on the working of Ansible. Ansible works through the configuration of client machines on a computer that has installed and configured Ansible components. Ansible uses normal SSH channels for communication to retrieve information from copy files, remote machines, and issue commands. Therefore, Ansible does not require the installation of any additional software on client computers.

Servers with exposed SSH port could come in the scope of Ansible’s configuration irrespective of the stage of the server in its lifecycle. Therefore, any computer suitable for administration through SSH could also be ideal for administration through Ansible. One of the important concerns of focus before you install Ansible is the modular approach of Ansible.

With the help of the modular approach, the extension is possible for leveraging functionalities of the main system to address particular scenarios. You can write modules in any language, and the communication between modules follows the standard JSON language. The configuration files are primarily documented in the YAML data serialization format because of the expressive nature and similarity to renowned markup languages.

Ansible can facilitate interaction with hosts through configuration scripts or command-line tools. The configuration scripts are known as Playbooks, which are a significant mention in almost every Ansible tutorial. System administrators and DevOps engineers should engage in the management of complex IT infrastructure on the cloud as well as across multiple sites.

For example, modern applications with a front-end service, database service, and an APO service would be very complex systems for management. Therefore, possibilities of error and higher time consumption become evident in the process of managing such complex systems. So, Ansible provides IT automation for the provisioning of IT infrastructure and deploying applications as well as managing changes in services configuration.

The approach followed by Ansible in IT automation depends largely on the ‘playbook.’ A closer look at any ansible tutorial will inform you that the Ansible playbook serves as a recipe. The Ansible playbooks describe the steps to establish your IT infrastructure, deploy your applications, and configure the services.

Ansible is highly popular because of the shallow learning curve that helps in getting started with Ansible quite easily. Another important highlight that you should note before you install Ansible is that it is agentless. Therefore, you don’t have to install and look after the maintenance of an Ansible client on the managed nodes. As a result, the management of Ansible updates becomes considerably simpler with this functionality.

Here we covered a detailed article on How Ansible Works?

Installing and Configuring Ansible

The most important concern before you install and configure Ansible is evident in the prerequisites. The first and foremost requirement is a control machine for the installation of Ansible. Now, you should know that you can install Ansible on Ubuntu, Debian, Linux Mint, or the Fedora Linux distributions.

The primary reason for this is the fact that a Windows machine cannot serve as a control machine. The control machine is important for managing other remote machines, and Ansible uses SSH for the same purpose. To install Ansible, you should also focus on the other prerequisite of two or more Ubuntu servers.

The two or more Ubuntu 18.04 servers will provide one Ansible server, and the remaining would serve as Ansible hosts. Every server should have a non-root user with SUDO privileges and a configured basic firewall. The next important element for ensuring Ansible config is the SSH keys created for non-root users on the Ansible server. The SSH key-pair is available saved to the default location “~ / .ssh / id_rsa,” and there is no need for password protection.

Step 1: Installation of Ansible

The first step to install Ansible on Ubuntu is to install the software on at least one machine. You can avail of the latest version of Ansible for Ubuntu by adding the personal package archive (PPA) of the project to your system. Another important concern before adding the project’s PPA to your system is the installation of the “software-properties-common” package. As a result, you can get features for easy management of Ansible and other independent software repositories. You can follow the commands mentioned below for installing the package.

$ Sudo apt update

$ Sudo apt install software-properties-common

Now, you have the add the Ansible PPA through the following command,

$ sudo apt-add-repository ppa:ansible/ansible

After adding the PPA, press “Enter” for completing the process to install Ansible. Now, you have to refresh the system’s package index again for ensuring that the package index knows all packages available in PPA. The command for refreshing system’s package index is,

$ Sudo apt update

After completing the update, you can install Ansible software with the following command,

$ Sudo apt install ansible

Now, your Ansible server contains all the software necessary for the administration of your hosts.

Step 2: Configuration of SSH Access to Ansible Hosts

The primary channel of communication between Ansible and client computers is SSH, as evident from the earlier part of this discussion. The next important aspect in every Ansible documentation refers to the configuration of SSH access to Ansible hosts. Ansible does have the ability to manage password-based SSH authentication. However, the use of SSH keys could help in maintaining simplicity. First of all, use the ‘cat’ command on your Ansible server for obtaining the contents in the SSH public key file of your non-root user on the terminal’s output. The command to achieve this task is,

$ cat ~/ .ssh/id_rsa.pub

Copy the output on the terminal to your clipboard and open a new terminal for connecting with one of the Ansible hosts using SSH. For example,

$ ssh trojan@ansible_host_ip

Now, the next step to configure after you install Ansible is to switch to the root user on the client machine. With the identity of the root user, you have to open the “authorized_keys” in the “~/ .ssh” directory. The command for the same is as follows,

# nano ~/ .ssh/authorized_keys

When the directory opens, you can complete Ansible config by pasting the SSH key of Ansible server user. Now, save the file and close the editor by pressing “Ctrl+X,” followed by pressing “Y” and then “Enter.” Finally, enter the “exit” command for returning to the non-root user of the host in the following format,

# exit

Most important of all, you should note that Ansible uses a Python interpreter in “/usr/bin/python” for running its modules. Therefore, you should install Python 2 on the host machine to ensure that Ansible could communicate with it. The following commands can help in updating the host’s package index and ensuring the installation of the “python” package.

$ Sudo apt update

$ Sudo apt install python

After you complete the installation, you need to run the “exit” command again for closing the connection with the client. The command for doing the same is,

$ exit

Ansible documentation suggests the repetition of this process for every server that you want to bring under control of your Ansible server. The next section deals with the configuration of the Ansible server for connecting with hosts using the Ansible’s “hosts” file.       

Ansible and Terraform are the two names that are prominent in the DevOps landscape now. Let’s understand the Ansible vs Terraform battle!

Step 3: Configuring the Ansible Hosts

Ansible maintains track of all servers through “hosts” file, and you can access the file with the following command,

$ Sudo nano /etc/ansible/hosts

The file would show various example configurations with a ‘#’ preceding each line. These examples can help in the configuration of various complex scenarios. The flexibility of the “hosts” file allows better opportunities for configuration. The syntax would be,

[group_name]

alias ansible_host=your_server_ip

“group_name” is the organizational tag that helps in finding any listed servers while “alias” implies reference to a particular server. After you install Ansible, you can access the servers from Ansible server with the following command.

$ ssh root@ansible_host_ip

You will not be asked for a password if you configure correctly. You can add a block containing details about the IP addresses of individual hosts to the “hosts” file. Now, you can configure Ansible for creating a file that informs all servers in the “servers” group for connecting as the root user. We could create a directory in the Ansible configuration structure known as “group_vars.” After you install Ansible, this folder helps in creating YAML-formatted files for each group that you want to configure. For examples,

$ Sudo mkdir /etc/ansible/group_vars

$ Sudo nano /etc/ansible/group_vars/servers

Once you are done with the YAML-formatted files, you can save and close the files. For specifying configuration details for every server irrespective of group association, you could input the details in a file at “/etc/ansible/group_vars/all.” Configuration of individual hosts is possible through creating files and naming them after aliases under the directory at “/etc/ansible/host_vars.”

Preparing for Ansible interview? Here’re the top 20 Ansible interview questions with detailed answers to crack the interview!

Final Words

The process to install Ansible appears very simple with the opportunity for effective configuration through following precise documentation. The power of Ansible as an IT automation tool provides considerable opportunities for revising conventional approaches for application development. The applications of Ansible are also considerably favorable as compared to other automation tools such as Puppet or Chef.

The ease of installing and setting up the production environment in Ansible signifies its advantages over other automation tools. Furthermore, there is no requirement of installing any agents on remote servers since Ansible relies only on SSH as its agent. So, enroll in our Ansible basics training course and get the best of automation simply and interactively with Ansible! 

About Dharmalingam N

Dharmalingam.N holds a master degree in Business Administration and writes on a wide range of topics ranging from technology to business analysis. He has a background in Relationship Management. Some of the topics he has written about and that have been published include; project management, business analysis and customer engagement.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top