22 C
Guru Unix /Linux and GNU supporter

How to install Ansible on Ubuntu 18.04?

Ansible is a free software platform to configure and manage computers automatically. In other words, it allows you to automate all the tedious and repetitive tasks that system administrators encounter.

Ansible is an open source project implemented in Python and has a modular architecture that can handle virtually any operating system, cloud environment and system management tool or framework.

It is included as part of the Linux Fedora distribution, inherited from Red Hat Inc. and is also available for Red Hat Enterprise Linux, CentOS and Scientific Linux through the Enterprise Linux Extras Packages (EPELs) as well as other operating systems, for example, Ubuntu and Debian.

Advantages and features

One of Ansible’s thanks to the competition is that you don’t have to install Agents or Servers. Just install ansible on your machine and you’re ready to go.

- Advertisement -

It does not require cumbersome and complicated configurations, its documentation is quite extensive and its configuration files explicit.

Another of its advantages is that it has an API and allows the development of plugins to extend its functionality.

Installing Ansible

Installing ansible is relatively easy as it is packaged for many of the most popular GNU/LINUX distributions. In the case of ubuntu 18.04 we have a PPA repository at our disposal that is ready to make the work even easier.

First of all we log in as a super user. Write in a terminal:

               sudo -i

Next we proceed to update the system, something always recommended to obtain even more security in our server:

1.- Upgrading the system
1.- Upgrading the system

The next step is to install the package software-properties-common:

                apt install software-properties-common

2.- Installing required packages
2.- Installing required packages

At the end of the installation, we have to add the PPA repository with the latest available version of Ansible.

               apt-add-repository ppa:ansible/ansible

apt-add-repository ppa:ansible/ansible
3.- Adding PPA

The next thing to do is to install the package that contains the program:

              apt install ansible

4.- Installing the main package
4.- Installing the main package

And now, that’s it.

Getting Started with Ansible

It is easy to take the first steps with Ansible, to demonstrate its use, we must have at least two machines, one that will be the main one and the other that will act as a node.

When Ansible is installed in one of the machines, we transform it into the main one. Remembering that there is no server as such, nor agents to configure.

The main machine cannot have Windows as its operating system, and must have python 2.6 or higher for effective installation.

For this tutorial the main machine has these features:

  • RAM Memory: 512mb
  • OS: Ubuntu server 18.04
  • IP Address:
  • Hard Drive: 20gb
  • Hostname: osradar1

And the second machine:

  • RAM Memory: 512mb
  • OS: Ubuntu server 18.04
  • IP Address:
  • Hard Drive: 20gb
  • Hostname: osradar2

Once we know this, we proceed to start the configuration process in order to execute the first command.

First of all we must create or modify the file /etc/ansible/hosts which is where the computers of our network are defined as well as the possibility of attaching them to groups. In this file we must place the address of the other node. To edit it, proceed to execute:

              nano /etc/ansible/hosts

5.-Editing the file hosts
5.-Editing the file hosts

Now we must make sure that we have SSH key authentication with the node. Write in a terminal:


6.- SSH-Keygen
6.- SSH-Keygen

Next, copy the public key to the node.

               ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

7.- Copying ssh key
7.- Copying ssh key

Now we can try to perform our first command from the main machine to the node to test the installation.

          ansible all -m ping -u angelo

8.- First command success
8.- First command success

We can also log in with the root user. Important if we want to execute tasks that require super user.

               ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Or any user.

Working with Playbooks

Playbooks are configuration files that allow us to deploy various tasks to different nodes, describing the steps to follow. Its writing is based on YAML and its format is.yml

For this tutorial a playbook will be made to install LAMP on the other node.

We create a playbook called lamp.yml and copy the following text into it:

                 nano lamp.yml

– hosts: all
user: root


-name: General | Installing packages
action: apt pkg={{ item }} state=installed
– php7.2
– apache2
– mariadb-server
– mariadb-client
– php7.2-mysql
– php-apcu
– php7.2-xmlrpc
– php7.2-soap
– php7.2-gd
– unzip
– python-mysqldb

– name: Apache2 |Enabling modules
action: command a2enmod rewrite vhost_alias

– name: Restart Apache
action: service name=apache2 state=restarted

9.- Creating the playbook
9.- Creating the playbook

Some components of the file are explained:

  • hosts: to whom the playbook is addressed. It can be one, or a group, or in this case all.
  • user: user that will execute it. As we know to install lamp we must be root user.
  • tasks: are the instructions to be executed on the node machine. It may be one or more as in this case. The first one installs the packages, the second one enables the apache modules and the third one restarts the services.

Remember that the /etc/ansible/hosts file can be used to create groups of nodes to speed up the selection process even more.

We proceed to run the playbook with the following command:

             ansible-playbook lamp.yml

10.- Running playbook
10.- Running playbook

11.- Running the playbook
11.- Running the playbook

As we can see, the 4 tasks were executed correctly.

This is just an outline of the great potential of Ansible that comes to give an open and simple solution to the world of process automation and tasks within any network.

Please share this article through your social networks.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article