Tutorial – Install Docker using Ansible on a remote server

Introduction

In this article you will learn how to install Docker on a remote server (in this tutorial we use Google Cloud) using Ansible without executing a single command on the remote server manually.

Docker is a virtualization software that runs on operation system level to easier isolate and manage running software and system infrastructure.

Ansible is an open source software and infrastructure automation programme which uses SSH among others to distribute it’s commands. If you want to find out more about how Ansible works click here.

Prerequisits

Install Ansible

First of all you need to install Ansible on your local computer.

# Install ansible

sudo apt-get update

sudo apt-get install software-properties-common

sudo apt-add-repository --yes --update ppa:ansible/ansible

sudo apt-get install ansible

# Test if it's working

ansible --version

# First Command

ansible all -i localhost, --connection=local -m ping

The last command basically tells Ansible to target all systems for the inventory host localhost and use your local console (not ssh) to run the module ping.

The response from Ansible should be


localhost | SUCCESS => {

"changed": false,

"ping": "pong"

}

Configuring Ansible for the remote server

Make sure that you have access to your remote server and placed your public SSH key there. Then edit your hosts file in /etc/ansible/hosts. This is the default file Ansible uses to look for any declared hosts (and groups) where it should execute the given commands remotely.

sudo nano /etc/ansible/hosts

Append the following lines to the file and save changes

[remote]
remote_test

[remote:vars]
ansible_host=IP_ADDRESS_OF_VIRTUAL_MACHINE
ansible_ssh_private_key_file=~/.ssh/YOUR_SSH_PRIVATE_KEY_FILE
ansible_user=YOUR_USERNAME

To test if Ansible can connect to your remote compute instance via SSH type the following command

ansible remote -m ping

The result should be

remote_test | SUCCESS => {

"changed": false,

"ping": "pong"

}

Create an Ansible playbook for Docker installation

Next we create a playbook for Ansible which is the normal way how to tell Ansible which commands to execute in which order on the remote server. The playbook is in a .yml file format and has a specific format to follow. You can find more out about playbooks in the official Ansible documentation.

nano install-docker-playbook.yml

Insert the following code in it.


---
- name: Install Docker
hosts: remote
vars:
CTOP_VERSION: "0.7.3"
DOCKER_COMPOSE_VERSION: "1.25.1"
DOCKER_PACKAGES:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
USER: "YOUR_USER_ON_THE_REMOTE_SERVER"

tasks:
- name: Update apt packages
apt:
update_cache: "yes"
force_apt_get: "yes"

- name: Install packages needed for Docker
apt:
name: "{{ DOCKER_PACKAGES }}"
state: present
force_apt_get: "yes"

- name: Add Docker GPG apt Key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Save the current Ubuntu release version into a variable
shell: lsb_release -cs
register: ubuntu_version

- name: Add Docker Repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ubuntu_version.stdout }} stable"
state: present

- name: Update apt packages
apt:
update_cache: "yes"
force_apt_get: "yes"

- name: Install Docker
apt:
name: "docker-ce"
state: present
force_apt_get: "yes"

- name: Test Docker with hello world example
shell: "docker run hello-world"
register: hello_world_output

- name: Show output of hello word example
debug:
msg: "Container Output: {{hello_world_output.stdout}}"

- name: Create docker group
group:
name: "docker"
state: present

- name: Adding user {{ USER }} to docker group
user:
name: "{{ USER }}"
groups: "docker"
append: "yes"

- name: Install Docker Compose
get_url:
url: https://github.com/docker/compose/releases/download/{{ DOCKER_COMPOSE_VERSION }}/docker-compose-Linux-x86_64
dest: "/usr/local/bin/docker-compose"
mode: 0755

- name: Install Ctop
get_url:
url: https://github.com/bcicen/ctop/releases/download/v{{ CTOP_VERSION }}/ctop-{{ CTOP_VERSION }}-linux-amd64
dest: "/usr/local/bin/ctop"
mode: 0755

- name: Reboot server in order for changes to take place
shell: "sleep 1 && reboot"
async: 1
poll: 0

This playbook basically tells Ansible to install Docker in several steps. We follow the basic Docker installation guide and also allow the specified user to execute Docker commands. Furthermore we install Docker-Compose (to be able to run multiple Docker containers at once) and Ctop a graphical user interface that shows you Docker metrics in real time.

Then execute it with

ansible-playbook install-docker-playbook.yml -l remote

Afterwards you should see some magic happen (can take a while) and in the output in your terminal you should find “Hello from Docker!” somewhere the debug message.

Congrats! You made it.

Final thoughts

As you see Ansible is a cool software to install other software remotely and scale it in the Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.