2023-10-22 15:33:05 +00:00
|
|
|
- name: Add official Docker APT key
|
|
|
|
ansible.builtin.get_url:
|
|
|
|
url: "{{ docker_apt_keyring_url }}"
|
|
|
|
dest: "{{ docker_apt_keyring }}"
|
|
|
|
checksum: "sha256:{{ docker_apt_keyring_hash }}"
|
2023-10-22 17:48:20 +00:00
|
|
|
mode: "644"
|
|
|
|
owner: root
|
|
|
|
group: root
|
2023-10-22 15:33:05 +00:00
|
|
|
when: docker_official
|
|
|
|
|
|
|
|
- name: Remove official Docker APT key
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: "{{ docker_apt_keyring }}"
|
|
|
|
state: absent
|
|
|
|
when: not docker_official
|
|
|
|
|
|
|
|
- name: Add/remove official Docker APT repository
|
|
|
|
ansible.builtin.apt_repository:
|
|
|
|
repo: >
|
|
|
|
deb [arch=amd64 signed-by={{ docker_apt_keyring }}]
|
|
|
|
{{ docker_apt_repo }} {{ ansible_distribution_release }} stable
|
|
|
|
state: "{{ 'present' if docker_official else 'absent' }}"
|
|
|
|
filename: "{{ docker_apt_keyring | regex_replace('^.*/', '') }}"
|
|
|
|
|
|
|
|
- name: Install/uninstall Docker from Debian repositories
|
2023-05-04 03:42:55 +00:00
|
|
|
ansible.builtin.apt:
|
2023-10-22 15:33:05 +00:00
|
|
|
name: ['docker.io', 'docker-compose', 'containerd', 'runc']
|
|
|
|
state: "{{ 'absent' if docker_official else 'present' }}"
|
|
|
|
autoremove: true
|
|
|
|
update_cache: true
|
|
|
|
|
|
|
|
- name: Install/uninstall Docker from Docker repositories
|
|
|
|
ansible.builtin.apt:
|
|
|
|
name: ['docker-ce', 'docker-ce-cli', 'containerd.io',
|
|
|
|
'docker-buildx-plugin', 'docker-compose-plugin']
|
|
|
|
state: "{{ 'present' if docker_official else 'absent' }}"
|
|
|
|
autoremove: true
|
2019-09-18 02:32:08 +00:00
|
|
|
update_cache: true
|
|
|
|
|
2023-09-30 02:18:59 +00:00
|
|
|
- name: Login to private registry
|
|
|
|
community.docker.docker_login:
|
|
|
|
registry_url: "{{ docker_login_url | default('') }}"
|
|
|
|
username: "{{ docker_login_user }}"
|
|
|
|
password: "{{ docker_login_pass }}"
|
|
|
|
when: docker_login_user is defined and docker_login_pass is defined
|
|
|
|
|
2022-05-27 03:50:14 +00:00
|
|
|
- name: Create docker-compose root
|
2023-05-04 03:42:55 +00:00
|
|
|
ansible.builtin.file:
|
2022-05-28 03:14:06 +00:00
|
|
|
path: "{{ docker_compose_root }}"
|
2022-05-27 03:50:14 +00:00
|
|
|
state: directory
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "500"
|
2022-05-27 03:50:14 +00:00
|
|
|
|
|
|
|
- name: Install docker-compose systemd service
|
2023-05-04 03:42:55 +00:00
|
|
|
ansible.builtin.template:
|
2022-05-27 03:50:14 +00:00
|
|
|
src: docker-compose.service.j2
|
|
|
|
dest: "/etc/systemd/system/{{ docker_compose_service }}@.service"
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "400"
|
2023-05-04 05:44:18 +00:00
|
|
|
notify: compose_systemd
|
2022-05-27 03:50:14 +00:00
|
|
|
|
2023-07-20 07:51:44 +00:00
|
|
|
- name: Create directories to clone docker-compose repositories
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: "{{ item }}"
|
|
|
|
state: directory
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "400"
|
2023-07-20 07:51:44 +00:00
|
|
|
loop:
|
|
|
|
- "{{ docker_repos_path }}"
|
|
|
|
- "{{ docker_repos_keys }}"
|
|
|
|
when: docker_compose_deploy is defined
|
|
|
|
|
|
|
|
- name: Generate OpenSSH deploy keys for docker-compose clones
|
|
|
|
community.crypto.openssh_keypair:
|
|
|
|
path: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
|
|
|
|
type: "{{ docker_repos_keytype }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
comment: "{{ ansible_hostname }}-deploy-key"
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "400"
|
|
|
|
state: present
|
|
|
|
when: docker_compose_deploy is defined
|
|
|
|
|
|
|
|
- name: Check for git installation
|
|
|
|
ansible.builtin.apt:
|
|
|
|
name: git
|
2023-07-20 07:51:44 +00:00
|
|
|
state: present
|
2023-07-22 03:52:18 +00:00
|
|
|
when: docker_compose_deploy is defined
|
2023-07-20 07:51:44 +00:00
|
|
|
|
|
|
|
- name: Clone external docker-compose projects
|
|
|
|
ansible.builtin.git:
|
|
|
|
repo: "{{ item.url }}"
|
|
|
|
dest: "{{ docker_repos_path }}/{{ item.name }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
version: "{{ item.version }}"
|
2023-10-21 01:30:25 +00:00
|
|
|
accept_newhostkey: "{{ item.accept_newhostkey | default(false) }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
gpg_whitelist: "{{ item.trusted_keys | default([]) }}"
|
|
|
|
verify_commit: "{{ true if (item.trusted_keys is defined and item.trusted_keys) else false }}"
|
2023-07-20 07:51:44 +00:00
|
|
|
key_file: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
|
|
|
|
loop: "{{ docker_compose_deploy }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.url }}"
|
|
|
|
when: docker_compose_deploy is defined
|
2023-07-20 07:51:44 +00:00
|
|
|
|
|
|
|
- name: Create directories for docker-compose projects using the systemd service
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: "{{ docker_compose_root }}/{{ item.name }}"
|
|
|
|
state: directory
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "400"
|
2023-07-20 07:51:44 +00:00
|
|
|
loop: "{{ docker_compose_deploy }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.name }}"
|
2023-07-20 07:51:44 +00:00
|
|
|
when: docker_compose_deploy is defined
|
|
|
|
|
2023-10-10 03:47:49 +00:00
|
|
|
- name: Synchronize docker-compose.yml
|
|
|
|
ansible.posix.synchronize:
|
2023-07-20 07:51:44 +00:00
|
|
|
src: "{{ docker_repos_path }}/{{ item.name }}/{{ item.path | default('docker-compose.yml') }}"
|
|
|
|
dest: "{{ docker_compose_root }}/{{ item.name }}/docker-compose.yml"
|
2023-10-10 03:47:49 +00:00
|
|
|
delegate_to: "{{ inventory_hostname }}"
|
|
|
|
register: compose_update
|
2023-10-22 15:33:05 +00:00
|
|
|
notify:
|
|
|
|
- compose_restart
|
|
|
|
- compose_enable
|
2023-10-10 04:14:52 +00:00
|
|
|
loop: "{{ docker_compose_deploy | default([]) }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.name }}"
|
2023-10-10 04:14:52 +00:00
|
|
|
when: docker_compose_deploy is defined and docker_compose_deploy | length > 0
|
2023-07-20 07:51:44 +00:00
|
|
|
|
|
|
|
- name: Set environment variables for docker-compose projects
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: docker-compose-env.j2
|
|
|
|
dest: "{{ docker_compose_root }}/{{ item.name }}/.env"
|
2023-10-22 15:33:05 +00:00
|
|
|
mode: "400"
|
2023-10-10 03:47:49 +00:00
|
|
|
register: compose_env_update
|
2023-10-22 15:33:05 +00:00
|
|
|
notify:
|
|
|
|
- compose_restart
|
|
|
|
- compose_enable
|
2023-10-21 01:30:25 +00:00
|
|
|
no_log: "{{ docker_compose_env_nolog | default(true) }}"
|
2023-07-20 07:51:44 +00:00
|
|
|
loop: "{{ docker_compose_deploy }}"
|
2023-10-10 03:47:49 +00:00
|
|
|
loop_control:
|
|
|
|
label: "{{ item.name }}"
|
2023-07-20 07:51:44 +00:00
|
|
|
when: docker_compose_deploy is defined and item.env is defined
|
|
|
|
|
2020-09-03 01:21:14 +00:00
|
|
|
- name: Add users to docker group
|
2023-05-04 03:42:55 +00:00
|
|
|
ansible.builtin.user:
|
2020-09-03 01:21:14 +00:00
|
|
|
name: "{{ item }}"
|
2019-09-18 02:32:08 +00:00
|
|
|
groups: docker
|
2020-08-01 00:52:57 +00:00
|
|
|
append: true
|
2020-09-03 01:21:14 +00:00
|
|
|
loop: "{{ docker_users }}"
|
2020-11-22 05:46:08 +00:00
|
|
|
when: docker_users is defined
|
2019-09-18 02:32:08 +00:00
|
|
|
|
2020-08-01 00:52:57 +00:00
|
|
|
- name: Start Docker and enable on boot
|
2023-05-04 03:42:55 +00:00
|
|
|
ansible.builtin.service:
|
2020-08-01 00:52:57 +00:00
|
|
|
name: docker
|
|
|
|
state: started
|
|
|
|
enabled: true
|
2023-10-22 15:33:05 +00:00
|
|
|
when: docker_managed | default(true)
|