Ansible: The Swiss Army Knife of Automation
Tags: devops, ansible, aws, docker
Published: 2024-04-26 02:11:12


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/taufq48h7ztu34md9fma.png)


## 📌Prerequisites:

- Install [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html)

- Install [Vscode](https://code.visualstudio.com/download)

## 🎯Workflow

1️⃣What we need to deploy the application❓
2️⃣Structure of the Project👷
3️⃣File Descriptions📂
4️⃣Playbook✏
5️⃣Check connectivity🌐 & Install Docker🐳
6️⃣Run docker app🚀



---

## 1️⃣What we need to deploy the application❓

Well, as the application is dockerized, the only thing we have to do is to **install docker** in the ec2 instance.

## 2️⃣Structure of the Project👷

```bash
ansible/
│
├── ansible.cfg
├── inventory.ini
│
├── key/
│   └── ec2_key.pem
│  
├── playbooks/
│   └── install_docker.yml
│   └── run_image.yml
```

- _ansible.cfg_📋 : _Defines the global configuration for running Ansible tasks in an environment._
- _inventary.ini📋: Define the list of hosts on which Ansible tasks will run. This file can contain groups of hosts and variables associated with those groups or individual hosts._
     - _/key/ec2_key.pem : This file will only be available on our localhost and contains the ssh key. Later I will add it as an environment variable._
     - _/playbooks/install_docker📋 : This file contain all the tasks to execute the docker installation_

## 3️⃣File Descriptions📂

> ansible.cfg

```bash
[defaults]
inventory = inventory.ini
host_key_checking = False  # optional: removes the SSH prompt 
deprecation_warnings=False  # optional: removes deprecation warning in playbooks
remote_user = ubuntu
#private_key_file = ./key/ec2_key.pem
```
>inventory.ini

```bash
[all]
node ansible_host=x.x.x.x ansible_user=ubuntu ansible_ssh_private_key_file=./key/ec2_key.pem 
```
>key/ec2_key.pem
 alerta bla blA

## 4️⃣Playbook✏

>playbooks/install_docker.yml

```yml
---
- name: Install docker
  hosts: all
  become: true

  tasks:
    - name: Install docker dependencies
      ansible.builtin.apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg-agent
          - software-properties-common
        update_cache: true

    - name: Add docker gpg key
      ansible.builtin.apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present
        keyring: /etc/apt/keyrings/docker.gpg

    - name: Add docker repository
      ansible.builtin.apt_repository:
        filename: docker
        repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename | lower }} stable
        state: present

    - name: Install docker engine
      ansible.builtin.apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-scan-plugin
          - docker-compose-plugin
        update_cache: true

    - name: Add user permissions
      shell: "usermod -aG docker ubuntu"
```
## 5️⃣Check connectivity🌐

`ansible -i inventory.ini -m ping all`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bkbf4p6u4ntpp8j7vpl.png)

Then, install docker🐳 on the host
`ansible-playbook -i inventory.ini playbooks/install_docker.yml`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7eylj7zyuonftap3tnjw.png)

## 6️⃣Run docker app🚀
This is the playbook to pull and run the docker image

```bash
---
- name: Download and run sharker3312/nodeapp:latest
  hosts: all
  become: true

  tasks:
    - name: Download Docker image
      ansible.builtin.docker_image:
        name: sharker3312/nodeapp:latest
        source: pull

    - name: Run Docker container
      ansible.builtin.docker_container:
        name: nodeapp
        image: sharker3312/nodeapp:latest
        state: started
        detach: yes
        ports:
          - "8000:8000"
```

`ansible-playbook -i inventory.ini playbooks/run_image.yml`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48h3ivorbu0lulftwhdt.png)

The web site: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf3r1ng6fm78ccuxapfs.png)


The purpose of using anisble software is to fully automate daily tasks. In this blog we observed how without having an interactive shell on the EC2 instance we were able to deploy a docker image.

---
<a href="https://linkedin.com/in/lesterdprez" rel="nofollow"><img src="https://camo.githubusercontent.com/d94940866c98cb4fca5783c4e8ac95776d2f52df6bbf3d5ab9e30d76836f30ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c696e6b6564496e2d2532333030373742352e7376673f6c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465" alt="LinkedIn" data-canonical-src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?logo=linkedin&amp;logoColor=white" style="max-width: 100%;"></a>