# How to install Ansible on Linux

In 
Published 2022-12-03

This tutorial explains to you how to install Ansible on Linux (CentOS 7).

In order to understand how to install Ansible on Linux (CentOS 7) we have to clarify several things:

  • Ansible is a software to automate tasks on other machines (Ansible send commands/ scripts on remote machine and this is done using ssh);
  • Ansible runs on a Linux machine only;
  • The machine where Ansible runs is named controller and from that machine all the deployments, commands are sent;
  • All the other machines are named targets;

So in order to have Ansible installed we need to installed it on a Linux machine.

For my example, I will create an environment on Virtual Box using Vagrant, but you can simply create 3 virtual machines named controller, target1 and target2. To see how Vagrant is installed you can take a look at my article named How to install Vagrant on Windows. If you use a virtual machine without Vagrant, you can download a virtual machine image from https://www.osboxes.org (for VMware & VirtualBox).

The first step (when using Vagrant) is to create a directory for the Vagrant project and download the Vagrantfile using the command:

vagrant init centos/7

Modify Vagrantfile as in the image below (for defining the virtual machines):

Start the virtual machines using the following command:

vagrant up

When the command completes you can go to the VirtualBox Manager and see that the machines are running:

It is possible that if you try to connect to one virtual machine using ssh to see the following screen/message:

In this case, go on each machine (from VirtualBox Manager) and edit the /etc/ssh/sshd_config file (PasswordAuthentivation must be yes):

Now you can connect to the controller and run the following commands:

  • yum -y install epel-release --> install EPEL repository for CentOS 7 (in my case)
  • yum -y update --> update the installed packages on CentOS
  • yum -y install ansible --> install Ansible on Linux (CentOS 7 in my case)
  • ansible --version --> test Ansible installation and the version installed

Now you can have another step to perform: Ansible must be enabled to send ssh request without restrictions.

For this you must generate the public/private key pair (the private key must remain on the controller) using the following command:

ssh-keygen

Now you can copy the public keys to the target machines using the following command (one command for each target machines):

ssh-copy-id root@111.0.0.11
ssh-copy-id root@111.0.0.12

Now you can test the ssh connections to the targets:

Now you are ready to use Ansible !