- name: Check if Docker CE is installed
  ansible.builtin.command: docker --version
  register: docker_check
  changed_when: false
  failed_when: false

- name: Download Docker installation script
  ansible.builtin.get_url:
    url: https://get.docker.com
    dest: /tmp/get-docker.sh
    mode: '0755'
  when: docker_check.rc != 0

- name: Install Docker CE
  ansible.builtin.shell: bash -c 'set -o pipefail && sh /tmp/get-docker.sh'
  args:
    creates: /usr/bin/docker
  when: docker_check.rc != 0

- name: Add user to docker group
  ansible.builtin.user:
    name: "{{ ansible_user }}"
    groups: docker
    append: true
  become: true
  when: docker_check.rc != 0

- name: Check if docker is running
  ansible.builtin.systemd:
    name: docker
    state: started
    enabled: true
  become: true
  register: docker_service

- name: Reload systemd
  ansible.builtin.systemd:
    daemon_reload: true
  become: true
  when: docker_service.changed

- name: Enable and start docker service
  ansible.builtin.systemd:
    name: docker
    state: started
    enabled: true
  become: true
  when: docker_service.changed
  notify: Reload systemd
