2022-07-10 10:50:57 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
- name: docker
|
|
|
|
hosts: all
|
|
|
|
tasks:
|
|
|
|
|
|
|
|
### Docker ###
|
|
|
|
|
|
|
|
- name: Create docker Group
|
|
|
|
ansible.builtin.group:
|
|
|
|
name: docker
|
|
|
|
state: present
|
|
|
|
gid: 1003
|
|
|
|
|
|
|
|
- name: Create docker User
|
|
|
|
ansible.builtin.user:
|
|
|
|
name: docker
|
|
|
|
comment: docker User
|
|
|
|
uid: 1003
|
|
|
|
group: docker
|
|
|
|
|
2023-02-09 13:52:16 +01:00
|
|
|
- name: Hide docker user from login screen
|
|
|
|
blockinfile:
|
|
|
|
path: /var/lib/AccountsService/users/docker
|
|
|
|
mode: "0444"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
create: yes
|
|
|
|
block: |
|
|
|
|
[User]
|
2023-04-12 09:29:57 +02:00
|
|
|
SystemAccount=true
|
2023-02-09 13:52:16 +01:00
|
|
|
backup: no
|
|
|
|
|
2022-07-10 10:50:57 +02:00
|
|
|
- name: Create docker dir
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: /home/docker
|
|
|
|
owner: docker
|
|
|
|
group: docker
|
|
|
|
state: directory
|
|
|
|
mode: '0750'
|
|
|
|
|
2022-07-14 12:33:36 +02:00
|
|
|
- name: Create docker-data dir
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: /home/docker/var-lib-docker
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
state: directory
|
|
|
|
mode: '0710'
|
|
|
|
|
2022-07-10 10:50:57 +02:00
|
|
|
- name: Create /var/lib/docker -> /home/docker/var-lib-docker symlink
|
|
|
|
ansible.builtin.file:
|
|
|
|
src: /home/docker/var-lib-docker
|
|
|
|
dest: /var/lib/docker
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
state: link
|
|
|
|
|
2023-04-12 09:29:57 +02:00
|
|
|
- name: Remove packages for docker from default repo
|
2022-07-10 10:50:57 +02:00
|
|
|
apt:
|
2023-04-12 09:29:57 +02:00
|
|
|
name:
|
2022-07-10 10:50:57 +02:00
|
|
|
- docker.io
|
|
|
|
- docker-compose
|
2023-04-12 09:29:57 +02:00
|
|
|
- containerd
|
|
|
|
- runc
|
|
|
|
state: absent
|
2022-07-10 10:50:57 +02:00
|
|
|
update_cache: no
|
|
|
|
install_recommends: no
|
|
|
|
|
2023-04-12 09:29:57 +02:00
|
|
|
- name: Add signing key
|
2023-07-25 10:31:06 +02:00
|
|
|
ansible.builtin.shell: curl -fsSL https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg | gpg --output "/usr/share/keyrings/gpg-pub-docker.gpg" --dearmor
|
|
|
|
args:
|
|
|
|
creates: /home/docker/nextcloud.{{inventory_hostname}}/env
|
|
|
|
|
|
|
|
#- name: Add signing key
|
|
|
|
# ansible.builtin.apt_key:
|
|
|
|
# url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
|
|
|
|
# state: present
|
2023-04-12 09:29:57 +02:00
|
|
|
|
2023-07-25 10:42:21 +02:00
|
|
|
- name: Add repository into sources list
|
|
|
|
ansible.builtin.apt_repository:
|
|
|
|
repo: "deb https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
|
|
|
|
state: absent
|
|
|
|
filename: docker
|
|
|
|
|
2023-04-12 09:29:57 +02:00
|
|
|
- name: Add repository into sources list
|
|
|
|
ansible.builtin.apt_repository:
|
2023-07-25 10:31:06 +02:00
|
|
|
repo: "deb [arch=amd64 signed-by=/usr/share/keyrings/gpg-pub-docker.gpg] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
|
2023-04-12 09:29:57 +02:00
|
|
|
state: present
|
|
|
|
filename: docker
|
|
|
|
|
|
|
|
- name: Packages for docker
|
|
|
|
apt:
|
|
|
|
name:
|
|
|
|
- docker-ce
|
|
|
|
- docker-ce-cli
|
|
|
|
- containerd.io
|
|
|
|
- docker-buildx-plugin
|
|
|
|
- docker-compose-plugin
|
|
|
|
- bridge-utils
|
|
|
|
- apache2-utils
|
2023-04-19 12:40:30 +02:00
|
|
|
- python3-docker
|
2023-04-12 09:29:57 +02:00
|
|
|
update_cache: yes
|
|
|
|
install_recommends: no
|
2023-06-05 12:30:27 +02:00
|
|
|
|
|
|
|
# https://github.com/nextcloud/docker/issues/1103
|
|
|
|
- name: writing daemon.json (ipv6 settings)
|
|
|
|
copy:
|
|
|
|
dest: "/etc/docker/daemon.json"
|
|
|
|
content: |
|
|
|
|
{
|
|
|
|
"ipv6": true,
|
|
|
|
"fixed-cidr-v6": "fdab::/64",
|
|
|
|
"experimental": true,
|
|
|
|
"ip6tables": true
|
|
|
|
}
|
2023-04-12 09:29:57 +02:00
|
|
|
|
2022-07-10 10:50:57 +02:00
|
|
|
- name: /usr/local/sbin/autoupdate.d/docker.update
|
|
|
|
blockinfile:
|
|
|
|
path: /usr/local/sbin/autoupdate.d/docker.update
|
|
|
|
mode: "0400"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
create: yes
|
|
|
|
marker: "# {mark} ANSIBLE MANAGED BLOCK"
|
|
|
|
block: |
|
2023-07-17 14:46:27 +02:00
|
|
|
# fix for creating notify.sh directory by docker (volume) if not exists
|
|
|
|
[ -d /usr/local/bin/notify.sh ] && rmdir /usr/local/bin/notify.sh
|
|
|
|
|
2022-07-10 10:50:57 +02:00
|
|
|
g_echo_ok "check for updates"
|
|
|
|
# Clean up docker images
|
|
|
|
docker system prune -af
|
2022-11-29 11:18:50 +01:00
|
|
|
find /home/docker -maxdepth 1 -mindepth 1 -type d | egrep -v "mailcow-dockerized$|.del$|.bak$|.old$|var-lib-docker$" | while read docker
|
2022-07-10 10:50:57 +02:00
|
|
|
do
|
|
|
|
[ -f "$docker/docker-compose.yml" ] || continue
|
|
|
|
g_echo_ok "$docker"
|
|
|
|
cd "${docker}"
|
2023-04-28 15:12:01 +02:00
|
|
|
if docker -l warn compose --ansi never pull --include-deps 2>&1 | grep "download complete"
|
2022-07-10 10:50:57 +02:00
|
|
|
then
|
|
|
|
g_echo_warn "Installiere $docker Update"
|
2023-04-28 15:12:01 +02:00
|
|
|
docker -l warn compose --ansi never down >$g_tmp/down 2>&1 || g_echo_error "$docker: docker compose pull fehlgeschlagen: $(cat $g_tmp/down)"
|
|
|
|
docker -l warn compose --ansi never up -d >$g_tmp/up 2>&1 || g_echo_error "$docker: docker compose up fehlgeschlagen: $(cat $g_tmp/up)"
|
2022-07-10 10:50:57 +02:00
|
|
|
fi
|
|
|
|
if [ -f Dockerfile ]
|
|
|
|
then
|
2023-04-28 15:12:01 +02:00
|
|
|
if docker -l warn compose --ansi never build --progress=plain --pull --no-cache --force-rm >$g_tmp/build 2>&1
|
2022-07-10 10:50:57 +02:00
|
|
|
then
|
2023-04-28 15:12:01 +02:00
|
|
|
docker -l warn compose --ansi never down >$g_tmp/down 2>&1 || g_echo_error "$docker: docker compose pull fehlgeschlagen: $(cat $g_tmp/down)"
|
|
|
|
docker -l warn compose --ansi never up -d >$g_tmp/up 2>&1 || g_echo_error "$docker: docker compose up fehlgeschlagen: $(cat $g_tmp/up)"
|
2022-07-10 10:50:57 +02:00
|
|
|
else
|
2023-04-19 09:15:58 +02:00
|
|
|
g_echo_error "$docker: docker compose build fehlgeschlagen: $(cat $g_tmp/build)"
|
2022-07-10 10:50:57 +02:00
|
|
|
fi
|
|
|
|
fi
|
2023-04-12 09:29:57 +02:00
|
|
|
done
|
2022-07-10 10:50:57 +02:00
|
|
|
backup: yes
|
|
|
|
validate: /bin/bash -n %s
|
|
|
|
|
|
|
|
- name: /usr/local/sbin/backup.d/docker.backup
|
|
|
|
blockinfile:
|
|
|
|
path: /usr/local/sbin/backup.d/docker.backup
|
|
|
|
mode: "0400"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
create: yes
|
|
|
|
marker: "# {mark} ANSIBLE MANAGED BLOCK"
|
|
|
|
block: |
|
|
|
|
### DB Backup
|
|
|
|
g_echo_ok "Starte Backup von MySQL-Datenbanken (Docker)"
|
|
|
|
|
|
|
|
DAYS=7
|
|
|
|
|
|
|
|
TIMESTAMP=$(date +"%Y%m%d%H%M")
|
|
|
|
CONTAINER=$(docker ps --format \{\{.Names\}\}:\{\{.Image\}\}| grep 'mysql\|mariadb' | cut -d":" -f1 | grep -v mailcow)
|
|
|
|
|
|
|
|
mkdir -p $BACKUPDIR
|
|
|
|
for i in $CONTAINER
|
|
|
|
do
|
2023-06-14 11:57:44 +02:00
|
|
|
# get credentials
|
2022-10-06 09:25:08 +02:00
|
|
|
MARIADB_DATABASE=$(docker exec $i env | egrep "MARIADB_DATABASE|MYSQL_DATABASE" | tail -n1 |cut -d"=" -f2)
|
|
|
|
MARIADB_PWD=$(docker exec $i env | egrep "MARIADB_PASSWORD|MYSQL_PASSWORD" | tail -n1 |cut -d"=" -f2)
|
|
|
|
MARIADB_USR=$(docker exec $i env | egrep "MARIADB_USER|MYSQL_USER" | tail -n1 |cut -d"=" -f2)
|
2023-06-14 11:57:44 +02:00
|
|
|
# get dump path
|
2023-06-19 11:35:08 +02:00
|
|
|
docker exec $i /usr/bin/test -x /usr/bin/mariadb-dump && DBDUMPCMD=/usr/bin/mariadb-dump
|
|
|
|
docker exec $i /usr/bin/test -x /usr/bin/mysqldump && DBDUMPCMD=/usr/bin/mysqldump
|
2023-06-16 15:36:58 +02:00
|
|
|
if [ -z "$DBDUMPCMD" ]
|
|
|
|
then
|
2023-06-16 15:37:35 +02:00
|
|
|
g_echo_error "No dbdumpcmd found in container $i! No backup created!"
|
|
|
|
continue
|
2023-06-16 15:36:58 +02:00
|
|
|
fi
|
2022-07-10 10:50:57 +02:00
|
|
|
g_echo "Sichere Datenbank $MARIADB_DATABASE DB aus $i";
|
2023-06-16 15:36:58 +02:00
|
|
|
docker exec $i $DBDUMPCMD --no-tablespaces -u $MARIADB_USR -p$MARIADB_PWD $MARIADB_DATABASE 2>${g_tmp}/dberr | gzip > $BACKUPDIR/$i-$MARIADB_DATABASE-$TIMESTAMP.sql.gz || g_echo_error "DB-Backup von $MARIADB_DATABASE war nicht erfolgreich $(cat ${g_tmp}/dberr)"
|
2022-07-10 10:50:57 +02:00
|
|
|
# dont delete last old backups!
|
|
|
|
OLD_BACKUPS=$(ls -1 $BACKUPDIR/$i*.gz |wc -l)
|
|
|
|
if [ $OLD_BACKUPS -gt $DAYS ]; then
|
|
|
|
find $BACKUPDIR -name "$i*.gz" -daystart -mtime +$DAYS -delete
|
|
|
|
fi
|
2023-04-12 09:29:57 +02:00
|
|
|
done
|
|
|
|
backup: yes
|
|
|
|
validate: /bin/bash -n %s
|
|
|
|
|
|
|
|
- name: docker-compose command/alias for backward compatibility - now docker compose
|
|
|
|
blockinfile:
|
|
|
|
path: /usr/local/sbin/docker-compose
|
|
|
|
mode: "0755"
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
create: yes
|
|
|
|
marker: "# {mark} ANSIBLE MANAGED BLOCK"
|
|
|
|
block: |
|
|
|
|
docker compose $@
|
2022-07-10 10:50:57 +02:00
|
|
|
backup: yes
|
|
|
|
validate: /bin/bash -n %s
|
|
|
|
|
2023-04-12 09:29:57 +02:00
|
|
|
- name: /usr/local/sbin/docker-compose shebang
|
|
|
|
lineinfile:
|
|
|
|
path: /usr/local/sbin/docker-compose
|
|
|
|
insertbefore: BOF
|
|
|
|
line: "#!/bin/bash -e"
|