Remote Server Management with Ansible

Ajanthan Eliyathamby 🇱🇰
The Startup
Published in
4 min readSep 10, 2019

--

This article explains about the Ansible Scripting which is used to manage multiple servers from a single server. This helps to automate the replication of configuration and deployments into multiple remote servers. Due to that the human errors while doing the same configuration into multiple servers will be reduced.

Contents

  • Understanding the Ansible Architecture
  • Installing Ansible in Ubuntu 18.04
  • Preparing the SSH connectivity between Ansible Server and other remote servers
  • Best practices for Ansible project directory structure
  • Demonstration of a basic use case

Understanding the Ansible Architecture

“Ansible”, as mentioned in the description through one server we are configuring or deploying to multiple servers. Based on that the architecture can be derived as below.

Installing Ansible in Ubuntu 18.04

To check the installed Ansible version.

Preparing the SSH connectivity between Ansible Server and other remote servers

To try out how the Ansible works, we can setup a VM Environment in our local machine and can play around it. Here, I’m using my host machine as Ubuntu 18.04 and installed a Ubuntu VM 18.04. While configuring the issue I face was the connectivity between the host and the VM. If you are using a fresh VM the below setting needs to done to enable the connectivity between VM and the host.

Go to Oracle VM Virtual Box and through File -> Host Network Manager… and then we will get a screen to create and after that you can see a screen like below:

After that go to particular installed VM Settings -> Network and configure the adapter as shown below:

Now when we execute the ifconfig, you can see that the VM also in the host machines network and for confirmation you can do a ping also.

Now we are fine with the Environment. It’s time to make the connectivity between host and VM through SSH.

As a first step we need to create the private and public keys. To generate execute the below command in the host which is the Ansible server in our case.

Then go to the VM and check whether the ssh service is running using the below command.

If not we need to install the openssh-server using the below command.

Then go to VM and .ssh folder and execute the below to create the authorized_keys file.

To check whether we can connect to SSH server, execute the below command.

Now we need to pass the public key to the remote server. We can do that using the below command.

Also we can use the below command to check whether the certificate added properly.

By executing the SSH command we can confirm our server in VM is accessible by the Host through SSH.

That’s it we are done with the connectivity. Now it’s time to move to the Ansible.

Best practices for Ansible project directory structure

Based on Ansible documentation there are two ways demonstrated on best practice of directory structure.

Note: Below information extracted from Ansible Documentation.

Structure — 1

The below script can be used to generate the structure. Note this is an extraction of the script provided in [1].

Structure — 2

The below script can be used to generate the structure. Note this is an extraction of the script provided in [2].

Demonstration of a basic use case

Here I’m demonstrating a simple scenario, just to get understand how the Ansible execution happens. For this using the directory Structure — 2, as it will give us a good entry point if we are moving to a large set of hosts.

Below image depicts the scenario, going to cover in the demonstration.

  1. As initial step in the host machine ( Ansible Server ) execute the script provided for directory structure — 1.
  2. Go to file inventories/staging/hosts and add the below.

3. Then go to roles/common/tasks/main.yml and add the below.

4. Create webservers.yml and add the below content. This will be in the project root folder.

5. Finally add the below to the site.yml, this is the init file where the execution starts.

Then we need to execute the below command to run the Ansible script.

That’s it, we have successfully ran through the basics of Ansible scripting. Hope this helps someone who is started to learn Ansible.

--

--

Ajanthan Eliyathamby 🇱🇰
The Startup

Associate Architect — Enterprise Integration | 14x WSO2 | 1x HashiCorp | 1× Azure | Runner-Up WCPY 2020 | https://ajanthane.blogspot.com