How to install and test Ansible on Linux

How to install and test Ansible on Linux

Ansible is a widely used configuration management tool written in Python language that can help configure one or more computers at the same time. Ansible can be used to perform the following tasks

  • Configure single or multiple nodes from scratch
  • deploy
  • Configuration changes
  • Patch
  • Service management
  • There are a lot more

Ansible is widely used because of its simplicity. Ansible code is also written in YAML language, no need to install an agent on the remote computer. Ansible uses port 22 (SSH) to connect to the remote computer and make the necessary changes.

Any computer that can be connected to a remote computer through port 22 can become a control node. You can install Ansible’s control node in it, and you can manage managed nodes through the control node.

Let’s take a look at the installation of Ansible on the controller node.

Install Ansible on CentOS 7:

Since Python is the only prerequisite for installing Ansible, let’s install Python by executing the following command.

$sudo yum install python3 -y

To install Ansible on CentOS 7, first make sure that the CentOS 7 EPEL repository is installed. Execute the following command to install the epel repository.

$sudo yum install epel-release

Update the system software package index by executing the following update command.

$sudo yum update -y

After installing the repository, install Ansible using the yum package manager. Execute the commands mentioned below to install Ansible.

$sudo yum install ansible -y

Verify that Ansible is correctly installed and its version.

$ansible -v

Install Ansible on CentOS 8:

Let’s take a look at the installation steps of CentOS 8. Let’s install python on CentOS 8.

$sudo dnf install python3

Once python is installed, let us install the EPEL repo by executing the following command.

$sudo dnf install epel-release -y

Update the system software package index by executing the following update command.

$sudo dnf update -y

Now, we are ready to install Ansible. Execute the following command to install Ansible.

$sudo dnf install ansible -y

Verify that Ansible is correctly installed and its version.

$ansible -v

Install Ansible on Ubuntu:

Today, Python is the default package in most Linux distributions. If you have not installed python, please execute the following command to install the python package.

$sudo apt-get install python3

To install Ansible in Ubuntu, first install the repository by executing the following command.

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

Update the system software package index by executing the following update command.

$sudo apt-get update -y

Now, install Ansible.

$sudo apt-get install -y ansible

Verify that Ansible is correctly installed and its version.

$ansible -v

Use Python PIP to install Ansible on CentOS, Debian and Ubuntu:

No matter what operating system is used, you can use the python package installer to install Ansible. Let us execute the following command to install python3-pip.Advertisements

For CentOS 7 and below:

$sudo yum install python3 python3-pip -y

For CentOS 8:

$sudo dnf install python3 python3-pip -y

For Ubuntu and Debian:

$sudo apt-get install python3 python3-pip

After installing python and pip, let us execute the following command on any operating system to install Ansible.

$sudo pip3 install ansible

Verify that Ansible is correctly installed and its version. ad

$ansible -v

Configure the Ansible controller host:

We must set up the “host” file before we can start communicating with other nodes. This file will have all the IP or host names of the managed nodes.

note: It is not always necessary to use the hosts file to connect to the managed node. However, every time we execute each command, we need to use the managed node server IP or host name.

Create the file with root authority by executing the following command (if it does not already exist):

$sudo touch /etc/ansible/hosts

Before writing to the file, let’s take a look and understand the example host file:

[group_name]alias ansible_ssh_host=your_node_server_ip

The host file should follow the above syntax. Let’s take a look at each parameter.

[group_name]: This parameter will create a group. All managed node IP addresses or host names under the group_name parameter will belong to the same group. For example, if we have multiple web servers in our infrastructure, we can add all IP addresses or host names of managed nodes here.

Alias: This parameter is used to provide an alias for the managed node server to identify the managed node server. For example, if we have multiple web servers in our infrastructure, we can give host1, host2, host3 as aliases. The main advantage of providing an alias is that when we execute the Ansible command to change the configuration of a single server, we can use the alias to identify and perform the required tasks on the server.

ansible_ssh_host = Your node server_ip: This parameter points the alias to the IP address or host name of the managed node.

We will use CentOS 8 as Ansible’s controlled node. As mentioned above, Ansible uses port 22 of the remote host to connect.

We will assume that the IP addresses of CentOS managed node servers are 192.168.0.2, 192.168.0.3 and 192.168.0.4.

To allow the Ansible controller node to communicate with the managed node, we must confirm that the managed node is accessible on port 22 (SSH port). Execute the following command to confirm the connectivity on all three managed nodes from the controller.

$ssh [email protected]192.168.0.2
$ssh [email protected]192.168.0.3
$ssh [email protected]192.168.0.4

You will be prompted to enter the password of the root user of all nodes. If you configure passwordless authentication between the Ansible controller and the managed node, Ansible will work very smoothly. Using different parameters in the Ansible command, password verification can also work.

After configuring the ssh connection between the Ansible controller and the managed node, let’s configure the hosts file to connect to all managed nodes. As mentioned earlier, we can add multiple node server aliases in a group. In this case, let’s add all three nodes and assign the aliases to host1, host2 and host3 respectively. After adding all the managed node details, our host file should look like this. Execute the following command to edit the host file we created earlier.

Open the file with root privileges by executing the following command:

$sudo vi /etc/ansible/hosts

Let’s add the configuration mentioned below to the host file.

[node_servers]host1 ansible_ssh_host=192.168.0.2host2 ansible_ssh_host=192.168.0.3host3 ansible_ssh_host=192.168.0.4

Here we add all managed node IPs and aliases to the name node_servers.

Now try to connect to the managed node from the Ansible controller.

$ansible -m ping node_servers

The above command uses the module ping to connect to the “node_servers” group we defined in the hosts file above.

You may encounter errors for different reasons.

  1. If you do not provide Ansible, by default, Ansible will try to connect to the managed node using your current username. If the user does not exist on the node server, you will receive the following error.
  2. If ssh port 22 is not open to connect on the managed node. (As mentioned earlier, Ansible connects on the ssh port)
  3. If the IP in the host file is incorrect.

If any of the above conditions fail, you will encounter the following error.

host1 | UNREACHABLE! => {    "changed": false,    "msg": "Failed to connect to the host via ssh.",    "unreachable": true}
host2 | UNREACHABLE! => {    "changed": false,    "msg": "Failed to connect to the host via ssh.",    "unreachable": true}
host3 | UNREACHABLE! => {    "changed": false,    "msg": "Failed to connect to the host via ssh.",    "unreachable": true}

We specifically told Ansible that it should use the james user to connect to managed nodes in the “node_servers” group. Create the group_vars directory as described below.

$sudo mkdir /etc/ansible/group_vars

The files in this directory are dedicated to configuring variables that can be used in Ansible playbooks.

Create a variable file for our settings by executing the following command:

$sudo vim /etc/ansible/group_vars/node_servers.yml

Add the following code in the file:

---ansible_ssh_user: james

YML files always start with “-” in the first line. When finished, let us save and close this file. Now, Ansible will always use the james user as the node_servers group, regardless of which current user you are using to run commands.

Check the connection of the managed node:

Now that we have set up the host and have enough configuration details to successfully connect to the managed node, we can try to use the same command we ran before.

$ansible -m ping servers 

Ansible will return the following output:

host1 | SUCCESS => {    "changed": false,    "ping": "pong"}
host2 | SUCCESS => {    "changed": false,    "ping": "pong"}

host3 | SUCCESS => {“changed”: no, “ping”: “pong”}

This is a basic test to ensure that Ansible establishes a connection with its hosting node.

in conclusion:

For managed nodes, it is not necessary to use the CentOS operating system. You can use the same test configuration above for CentOS, RedHat, Ubuntu and any other Linux distributions.

ad

Sidebar