📌Prerequisites:
🎯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👷
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
[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
[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
---
- 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
Then, install docker🐳 on the host
ansible-playbook -i inventory.ini playbooks/install_docker.yml
6️⃣Run docker app🚀
This is the playbook to pull and run the docker image
---
- 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
The web site:
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.
Top comments (0)