Creating Ansible role to install Docker on VPS

avatar

Good day dear mates! In this post I'd like to show you how it's possible to create an Ansible role which allows you to install Docker on VPS. And it'll be the second post from series where I'd like to describe and write for myself topics about CI/CD, IAC and server related things.

Here I won't write many words about what is ansible. For those who not familiar with Ansible shortly

Ansible is an open source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more.
But let's move to writing role!
First of all it's needed that Ansible already has been installed on your host machine. In other case you can do it by yourself following next commands

sudo apt update
sudo apt install ansible

Also you can install ansible with pip. How to do it you can find in docs on official site
`
To verify Installationyou can check the installed version of Ansible to ensure it is correctly installed

ansible --version

Also I suggest you to install Ansible-galaxy to create structure of playbook just with one command

ansible-galaxy role init my_role_name

Now let's look what we have in root directory we have several folders which contains yml files where written variables and tasks for ansible.

Снимок экрана от 2025-01-24 00-15-29.png
!!!
First of all to install Docker following steps described in the following instructions must be followed.
First of all recomended to remove old packages with next task in main.yml file from tasks directory

- name: Docker | remove old packeges
  ansible.builtin.apt:
    name:
      - docker 
      - docker-engine 
      - docker.io 
      - containerd 
      - runc
    state: absent

Then we need to add Docker's official GPG key with folowing tasks:

- name: Docker keyrings directory
  ansible.builtin.file:
    path: /etc/apt/keyrings
    mode: 0755
    state: directory
    owner: root
    group: root

- name: Docker add apt key
  ansible.builtin.apt_key:
    url: "{{ docker_gpg_url }}/gpg"
    keyring:  "{{ docker_gpg_path }}"
    state: present

After it we need to add the repository to Apt sources

- name: Docker | add apt source
  ansible.builtin.apt_repository:
    repo: "deb [arch=amd64 signed-by={{ docker_gpg_path }}] {{ docker_gpg_url }} {{ ansible_distribution_release }} stable"
    state: present

And finally instalation task

- name: Docker | install packages
  ansible.builtin.apt:
    name:
      - docker-ce
      - docker-ce-cli
      - docker-buildx-plugin
      - docker-compose-plugin
    update_cache: true

And after all installation tasks we need to start service

- name: Docker | Service start
  ansible.builtin.service:
    name: docker
    state: started
    enabled: true

With previous task we finished to discribe instalation tasks for our Docker installation role. But that's not all. As you could see in that some values were taken in {{ value }} it means that this variables were determinated in other place and can be dynamically changed in all places when their value was changed in defaults or vars directory main.yml file.
So I in this role I determinated docker_gpg_url and docker_gpg_path variables in main.yml file from defaults directory.

docker_gpg_url: "https://download.docker.com/linux/ubuntu"
docker_gpg_path: "/etc/apt/keyrings/docker.gpg"

Also additionally in role you can add user to docker group to have permission to work with it using for example next task

- name: Add user to the docker group
  user:
    name: "{{ ansible_ssh_user }}"
    groups: docker
    append: yes

Besides it to run role next we need to write simple playbook which will call our role, determinate host on which docker will be deployed. First of all let's create file host where we include ip address and user with which ansible will login to vps.

[host]
 51.250.70.179 ansible_ssh_user=ansible

And at the end playbook itself.

--
- hosts: all
  become: true
  roles:
    docker

So now all is what we need is to run playbook with command

ansible-playbook -i host -u ansible docker.yaml

As a result we'll see following output

To check correctly docker was installed or not on your VPS you can login on your server and then run simple command like

docker run hello-world

As a result you should see similar message like it shown on picture bellow

So in this tutorial it was a small example how it is possible to install Docker with ansible.
First post with a Simple Gitlab CI/CD pipline example can be found here



0
0
0.000
0 comments