initial commit

This commit is contained in:
cuqmbr 2025-01-10 11:13:46 +02:00
commit 4043862397
Signed by: cuqmbr
GPG Key ID: 0AA446880C766199
8 changed files with 181 additions and 0 deletions

71
README.md Normal file
View File

@ -0,0 +1,71 @@
Docker Container
=========
Deploy docker containers on Ubuntu 24.04.
Requirements
------------
Docker engine must be insalled on the host this role will run on.
Role Variables
--------------
The following variables will be equivalent to `docker.io/ubuntu/nginx:latest`.
Docker registry base url (default: docker.io):
`docker_container_registry: docker.io`
Docker rebpository namespace (default: ""):
`docker_container_repository_namespace: ubuntu`
Docker repository name (default: ubuntu):
`docker_container_repository_name: nginx`
Docker container tag (default: latest):
`docker_container_repository_tag: latest`
Docker container options to pass to docker run command (default: undefined)
`docker_container_run_options: "-p 8080:8080"`
Default image: docker.io/ubuntu:latest
Dependencies
------------
No dependencies.
Example Playbook
----------------
---
- hosts: goapp
vars:
docker_container_registry: docker.io
docker_container_repository_name: ubuntu
docker_container_repository_tag: latest
docker_container_run_options: "-p 8080:8080"
pre_tasks:
- name: Update apt cache.
ansible.builtin.apt:
update_cache: true
cache_valid_time: 86400
roles:
- role: cuqmbr.docker_container
TODO
----
Add support for other Linux distrubitions:
- RHEL: Almalinux, RockyLinux 9 and 8
- Debian: Debian 12 and 11, Ubuntu 24.04 and 22.04
- Archlinux
Make role more generic:
- Add dockerd parameters configuration
- Add `docker container create` parameters configuration

8
defaults/main.yml Normal file
View File

@ -0,0 +1,8 @@
---
docker_container_registry: docker.io
docker_container_repository_namespace: ""
docker_container_repository_name: ubuntu
docker_container_repository_tag: latest
# docker_container_run_options: "-p 8080:8080"

7
handlers/main.yml Normal file
View File

@ -0,0 +1,7 @@
---
- name: Restart docker container service
ansible.builtin.systemd_service:
name: "docker_{{ docker_container_repository_name }}.service"
state: restarted
daemon_reload: true

11
meta/main.yml Normal file
View File

@ -0,0 +1,11 @@
---
galaxy_info:
role_name: docker_container
author: cuqmbr
description: Generic docker container deployment.
# issue_tracker_url: http://example.com/issue/tracker
license: MIT
min_ansible_version: "2.1"
galaxy_tags: []
dependencies: []

View File

@ -0,0 +1,18 @@
---
- name: Converge
hosts: all
vars:
docker_container_registry: docker.io
docker_container_repository_name: ubuntu
docker_container_repository_tag: latest
pre_tasks:
- name: Update apt cache.
ansible.builtin.apt:
update_cache: true
cache_valid_time: 86400
roles:
- role: cuqmbr.docker
- role: cuqmbr.docker_container

View File

@ -0,0 +1,20 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu2404
image: docker.io/geerlingguy/docker-ubuntu2404-ansible:latest
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
provisioner:
name: ansible
# options:
# ask-vault-pass: true
verifier:
name: ansible

32
tasks/main.yml Normal file
View File

@ -0,0 +1,32 @@
---
- name: Form a full container name without repository namespace.
ansible.builtin.set_fact:
docker_container_name: "{{ docker_container_registry }}/\
{{ docker_container_repository_name }}:\
{{ docker_container_repository_tag }}"
when: docker_container_repository_namespace|length == 0
- name: Form a full container name with repository namespace.
ansible.builtin.set_fact:
docker_container_name: "{{ docker_container_registry }}/\
{{ docker_container_repository_namespace }}/\
{{ docker_container_repository_name }}:\
{{ docker_container_repository_tag }}"
when: docker_container_repository_namespace|length > 0
- name: Install systemd service file.
ansible.builtin.template:
src: docker_container.service.j2
dest: "/etc/systemd/system/\
docker_{{ docker_container_repository_name }}.service"
owner: root
group: root
mode: "0644"
notify:
- Restart docker container service
- name: Enable docker container service.
ansible.builtin.service:
name: "docker_{{ docker_container_repository_name }}.service"
enabled: true

View File

@ -0,0 +1,14 @@
[Unit]
Description={{ docker_container_name }} docker container
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=/usr/bin/docker pull {{ docker_container_name }}
ExecStart=/usr/bin/docker run --rm --name %n{% if docker_container_run_options is defined %} {{ docker_container_run_options }}{% endif %} {{ docker_container_name }}
ExecStop=/usr/bin/docker stop %n
[Install]
WantedBy=multi-user.target