Atlantic.Net Blog

How to Install Ansible Server on Ubuntu 18.04

Hitesh Jethva
by Atlantic.Net (64posts) under VPS Hosting
0 Comments

Ansible is an automation tool set that works extremely well with Linux-based servers, but is also compatible with other cloud servers, such as Windows and VMware (to name a few). Ansible is an agentless application that only requires Python to function, which is a default application in nearly all Linux distributions.

Ansible its great at automating administrative tasks and creating a desired state infrastructure (ensuring all servers are the same).

Ansible works using the SSH protocol, so you don’t need any software or agent on remote servers. It is a great alternative to other automation tools, including Chef and Puppet.

In this article, we will show you how to install and configure Ansible on Ubuntu 18.04 server.

Prerequisites

  • A fresh Ubuntu 18.04 VPS on the Atlantic.Net Cloud Platform.
  • Preferably, you will have a few Linux test servers (Ubuntu or Debian).
  • A root password configured on your server.

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 18.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged into your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Configure SSH Key-based Authentication

Ansible uses SSH to retrieve information from the remote hosts. You will need to set up key-based authentication for the remote hosts so the Ansible server can communicate with remote hosts without entering SSH password.

First, generate an SSH key on Ansible server with the following command:

ssh-keygen -t rsa

You should get the following output:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HzUXrR+ltn9w0mwz5+hKeFzEOPcJrLTKi9C72Y7elAE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|              .. |
|            .o .o|
|       E   .=o=o.|
|        . ..o*=o.|
|        S..o ..*o|
|      . ..++ .+oB|
|     . . =o +  O+|
|      . O .o  . +|
|      .Bo=  .o. .|
+----[SHA256]-----+

Next, copy the generated key to the remote hosts with the following command:

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

Next, check the SSH password-less login with the following command:

ssh [email protected]

Step 3 – Install Ansible

By default, Ansible is not available in the Ubuntu 18.04 default repository, so you will need to add Ansible PPA to your system.

You can add Ansible PPA with the following command:

apt-add-repository ppa:ansible/ansible

After adding Ansible PPA, update the repository and install Ansible with the following command:

apt-get update -y
apt-get install ansible -y

Once the installation has been completed, you can proceed to the next step.

Step 4 – Configure Ansible Hosts

Next, you will need to configure your Ansible hosts file to define the remote servers. You can edit the /etc/ansible/hosts with the following command:

nano /etc/ansible/hosts

You can add single hosts or group of hosts, as shown below:

[webservers]
webserver1 ansible_ssh_host=192.168.1.2 ansible_ssh_port=22 ansible_ssh_user=root
webserver2 ansible_ssh_host=192.168.1.3 ansible_ssh_port=22 ansible_ssh_user=root

[databaseservers]
dbserver1 ansible_ssh_host=192.168.1.4 ansible_ssh_port=22 ansible_ssh_user=root

Save and close the file when you are finished.

In the above file, webserver1 and webserver2 are used to define a webserver IP address, SSH port and username, while dbserver1 is used to define a database server IP Address, SSH port and username.

Next, disable the Python warning by editing the file /etc/ansible/ansible.cfg:

nano /etc/ansible/ansible.cfg

Add the following line below [defaults]:

deprecation_warnings=False

Save and close the file when you are finished.

At this point, your Ansible server is ready to manage your web server and database server.

Step 5 – Test Ansible

After setting up the Ansible server, it’s time to test the connectivity of all Ansible hosts.

On the Ansible server, run the following command to test the connectivity of all hosts:

ansible -m ping all

If everything is fine, you should get the following output:

To test the connectivity of specific hosts like dbserver1, run the following command:

ansible -m ping dbserver1

You should see the following output:

To test the connectivity of a group of hosts like [webservers], run the following command:

ansible -m ping webservers

You should see the following output:

Step 6 – Ansible Advanced Commands

You can execute any command on a remote server over SSH from the Ansible server.

For example, to check disk usage of all Ansible hosts, run the following command:

ansible -m shell -a "df -h" all

This command will execute “df -h” command on all remote hosts and give the following output:

You can list all databases on dbserver1 hosts with the following command:

ansible -m shell -a "mysql -u root [email protected] -e 'show databases;'" dbserver1

You should see the following output:

Conclusion

In the above tutorial, we learned how to install Ansible on Ubuntu 18.04 server. We also learned how to manage multiple servers with Ansible. You can now easily manage your entire infrastructure with Ansible – try it out with VPS Hosting from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources