# vim: ft=yaml.ansible
# code: language=ansible
---
- name: Add Docker repository
  ansible.builtin.yum_repository:
    name: docker-ce-stable
    description: Docker CE Stable - $basearch
    file: docker-ce
    baseurl: https://download.docker.com/linux/centos/$releasever/$basearch/stable
    gpgkey: https://download.docker.com/linux/centos/gpg
    gpgcheck: true
    enabled: true
    state: present

- name: Install Docker and Docker Compose
  ansible.builtin.dnf:
    name:
      - docker-ce
      - docker-ce-cli
      - docker-compose-plugin
      - containerd.io
    state: present

- name: Create Docker data root directory
  ansible.builtin.file:
    path: "{{ docker_data_root }}"
    owner: root
    group: root
    mode: u=rwx,g=x,o=
    seuser: system_u
    serole: object_r
    setype: container_var_lib_t
    selevel: s0
    state: directory

- name: Copy Docker daemon config file
  ansible.builtin.template:
    src: daemon.json.j2
    dest: /etc/docker/daemon.json
    owner: root
    group: root
    mode: u=rw,g=r,o=r
  notify: Reload Docker daemon

- name: Ensure Docker daemon is enabled and running
  ansible.builtin.service:
    name: docker
    enabled: true
    state: started

- name: Configure cron job to prune unused Docker data daily
  ansible.builtin.cron:
    name: Prune unused Docker data
    cron_file: ansible_docker_prune
    job: 'docker system prune -fa && docker volume prune -fa'
    special_time: daily
    user: root
    state: present