Ansible Made Easy
Tags: ansible, beginners, devops, aws
Published: 2022-10-02 17:27:26

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](https://www.ansible.com/#) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c03o15akz2oevxf6bh6w.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


![automate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wog733dkhfkj7r10o90.png)

According to  [official documentation](https://www.ansible.com/overview/it-automation), 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](https://github.com/Damola12345/nautilus-devops/tree/master/week5).



![diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ch50t2xkqz2j1lme6ckh.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

```
![keypair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jnee5kktqg9tlytbm38.png)

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

![server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjaxb8hh7ybouu8ut67n.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). 

![Inventory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q755290uk13n1tm0q6zr.png)

Create playbook.

![playbook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb3cn3xyjwwjdi4s3ua1.png)

Create  `ansible.cfg` file. 

![ansible.cfg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scl618v25wqzmtqqeb52.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
```

![ping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2th7k08xslh0spskcybr.png)

Command to execute the playbook.
```
ansible-playbook -i inventory playbook.yml
```
![playbook.yml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rvclwjwlpp20udxyb23.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](https://docs.ansible.com/) and also checkout this blog on [Ansible](https://spacelift.io/blog/ansible-tutorial).