DEV Community

Cover image for Ansible Made Easy
Adedamola Ajibola
Adedamola Ajibola

Posted on • Edited on

Ansible Made Easy

This piece will walk you around the definition of Ansible, how it works and the problems it solves, and a simple tutorial using Ansible to install these applications on 3 Ubuntu servers - curl, http, nginx, apache.

Ansible

Ansible is an open-source automation configuration management and application deployment tool written in Python. It helps to reduce managerial overhead by automating the deployment of the application and managing IT infrastructure.

Ansible

It runs on UNIX-like systems and can provision and configure both UNIX-like and Windows systems. Ansible does not require an agent to run on a target system, unlike other automation software. It leverages on the SSH connection and python interpreter to perform the given tasks on the target system.

Ansible can be installed on a cloud server to manage other cloud servers from a central location, or it can also be configured to use on a personal system to manage cloud or on-premises systems.

How it works

Ansible is all about automation, it needs a directive to achieve all tasks and it is easy and simple to do version control.

Ansible consists of control nodes and host nodes. Ansible is installed on the control nodes, while the host nodes is managed by the control nodes. It uses no agents and it's so easy to deploy, above all it uses a simple language (YAML in the form of Ansible Playbooks).

Problems It Solves

automate

According to official documentation, Ansible delivers simple IT automation that ends repetitive tasks and frees up DevOps teams for more strategic work.

Next, is a simple tutorial on Ansible and the aim of this tutorial is to install these applications on 3 Ubuntu servers curl, nginx, apache. Check out this repo.

diagram

  • The first step is to launch an EC2 instance(ubuntu) and name it Ansible-master.

  • Update the repository cache by running the command.

sudo apt update && sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode
  • Run the following command to install the latest version of Ansible on Ansible-master.
sudo apt install  ansible -y ansible –version
Enter fullscreen mode Exit fullscreen mode

Generate SSH Key Pair

We can connect to remote hosts using a password through Ansible. It is recommended to set up key-based authentication for easy and secure logins.

SSH into the server and run this command to create key pairs on the ansible-master.

ssh username@host.com 
cd ~/.ssh 
ssh-keygen -t rsa -b 2048
Enter fullscreen mode Exit fullscreen mode

Run the ls command to see two keys generated in the .ssh folder. The public key contains a .pub extension while the private key doesn’t.

ls
Enter fullscreen mode Exit fullscreen mode

Copy & paste the public key into a newly Import key pair on the console.

cat xx.pub 
ansible key

Enter fullscreen mode Exit fullscreen mode

keypair

Launch three new servers and use the newly created key pair. ansible key

server

Test connection (login to the server by running the command without asking for a password).

ssh  ubuntu@54.xxx.8.xxx
Enter fullscreen mode Exit fullscreen mode

Create inventory file (contains IP address assigned to servers).

Inventory

Create playbook.

playbook

Create ansible.cfg file.

ansible.cfg

We have done the minimal configuration required to connect to the remote machine using Ansible. Run the following command on (ansible-master) to ping the host using Ansible ping module.

ansible all --key-file  ~/.ssh/id_rsa -i inventory -m ping
Enter fullscreen mode Exit fullscreen mode

ping

Command to execute the playbook.

ansible-playbook -i inventory playbook.yml
Enter fullscreen mode Exit fullscreen mode

playbook.yml

Summary

Ansible automates everything across your IT infrastructure and makes it look simple. We learned how to install Ansible on Ubuntu and also saw how to connect to remote servers using SSH key-based authentication, Ran some simple Ansible commands to connect to the servers. You can learn more about Ansible from the documentation hosted at Ansible Documentation and also checkout this blog on Ansible.

Top comments (0)