Ansible Made Easy

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.

Screenshot 2022-09-25 at 12.11.50.png

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

Screenshot 2022-09-25 at 12.11.21.png

According to the 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.

ansible.jpg

  • 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
    
  • Run the following command to install the latest version of Ansible on Ansible-master.

    sudo apt install  ansible -y ansible –version
    

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

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

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

cat xx.pub 
ansible key

Screenshot 2022-10-02 at 13.22.21.png

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

Screenshot 2022-08-02 at 18.38.47.png

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

ssh  ubuntu@54.xxx.8.xxx

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

Screenshot 2022-10-02 at 13.28.26.png

Create playbook.

Screenshot 2022-10-02 at 13.30.54.png

Create ansible.cfg file.

Screenshot 2022-10-02 at 13.28.42.png

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

Screenshot 2022-08-02 at 18.35.38.png

Command to execute the playbook.

ansible-playbook -i inventory playbook.yml

Screenshot 2022-08-02 at 18.41.01.png

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.