---
- name: Setup Docker systems
  hosts: docker
  tasks:
    - name: Docker Repo
      ansible.builtin.yum_repository:
        name: docker-ce-stable
        description: Docker CE Stable - x86_64
        baseurl: https://download.docker.com/linux/rhel/$releasever/$basearch/stable
        gpgkey: https://download.docker.com/linux/rhel/gpg
      when: ansible_facts['os_family'] == "RedHat"

    - name: Docker Packages
      ansible.builtin.package:
        name: 
          - docker-ce
          - docker-ce-cli
          - containerd.io 
          - docker-buildx-plugin 
          - docker-compose-plugin
        state: present
      when: ansible_facts['os_family'] == "RedHat"
        
    - name: Hawser
      include_tasks: tasks/hawser.yml

    - name: Create systemd override directory
      file:
        path: /etc/systemd/system/docker.service.d
        state: directory
        mode: '0755'
        owner: root
        group: root

    - name: Deploy daemon.json
      template:
        src: templates/docker-daemon.json
        dest: /etc/docker/daemon.json
        owner: root
        group: root
        mode: '0644'
      notify: Restart docker

    - name: Deploy override.json
      template:
        src: templates/docker-override.conf
        dest: /etc/systemd/system/docker.service.d/override.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart docker


    - name: Open Firewall port
      firewalld:
        # service: docker-registry
        port: 2376/tcp
        permanent: true
        state: enabled
        immediate: yes
      when: ansible_facts['os_family'] == "RedHat"

    - name: Close Firewall port
      firewalld:
        # service: docker-registry
        port: 2375/tcp
        permanent: true
        state: disabled
        immediate: yes
      when: ansible_facts['os_family'] == "RedHat"

    - name: Create /volumes directory
      file:
        path: /volumes
        state: directory
        # mode: '0755'
        owner: root
        group: root

    - name: Mount and bind a volume
      ansible.posix.mount:
        path: /volumes
        src: /var/lib/docker/volumes
        opts: bind
        state: mounted
        fstype: none

    - name: Enable Docker
      ansible.builtin.systemd:
        name: docker
        state: started
        enabled: yes

  handlers:
    - name: Restart docker
      service:
        name: docker
        state: restarted


