---
#--------------------------------------------------------------#
# Install docker                                [docker_install]
#--------------------------------------------------------------#
- name: install docker and docker-compose
  tags: docker_install
  environment: "{{ proxy_env | default({}) }}"
  ignore_errors: true  # HOTFIX for EL8 Docker installation
  package: name={{ item }} state=present
  loop:
    - docker-ce
    - docker-compose-plugin


#--------------------------------------------------------------#
# Add admin user to the Docker group              [docker_admin]
#--------------------------------------------------------------#
- name: add admin user to docker group
  ignore_errors: true
  tags: docker_admin
  user: name={{ item }} append=true groups=docker
  loop:
    - "{% if node_user == 'root' %}{{ node_admin_username }}{% else %}{{ node_user }}{% endif %}"
    - "{{ node_admin_username }}"


#--------------------------------------------------------------#
# create docker dir                                 [docker_dir]
#--------------------------------------------------------------#
- name: create docker dir
  tags: docker_dir
  when: docker_data is defined and docker_data != ''
  file: path={{ docker_data }} state=directory mode=0755


#--------------------------------------------------------------#
# config docker                                  [docker_config]
#--------------------------------------------------------------#
- name: config docker
  tags: docker_config
  block:
    - name: create /etc/docker config dir
      file: path=/etc/docker state=directory mode=0755

    - name: render /etc/docker/daemon.json
      template: src=daemon.json.j2 dest=/etc/docker/daemon.json


#--------------------------------------------------------------#
# launch docker                                  [docker_launch]
#--------------------------------------------------------------#
- name: launch docker systemd service
  tags: docker_launch
  systemd: name=docker state=restarted enabled=yes daemon_reload=yes


#--------------------------------------------------------------#
# Register docker monitor target               [docker_register]
#--------------------------------------------------------------#
# /infra/targets/docker/{{ ip }}.yml
- name: register docker as victoria target
  tags: [ docker_register, register, add_metrics ]
  ignore_errors: true
  delegate_to: '{{ item }}'
  loop: '{{ groups["infra"]|default([]) }}'
  copy:
    dest: "/infra/targets/docker/{{ inventory_hostname }}.yml"
    owner: victoria
    group: infra
    mode: '0640'
    content: |
      # {{ inventory_hostname }} 
      - labels: { ip: {{ inventory_hostname }} , ins: {{ nodename }} , cls: {{ node_cluster|default('nodes') }}  }
        targets: [ {{ inventory_hostname }}:{{ docker_exporter_port }} ]


#--------------------------------------------------------------#
# load docker images cache if exists              [docker_image]
#--------------------------------------------------------------#
- name: load docker images
  tags: docker_image
  ignore_errors: true
  block:

    - name: pull docker images
      when: docker_image is defined and docker_image|length > 0
      command: docker pull "{{ item }}"
      loop: "{{ docker_image }}"

    - name: find local docker image cache
      find:
        paths: "{{ docker_image_cache | dirname }}"
        patterns: "{{ docker_image_cache | basename }}"
        use_regex: no
      register: docker_images

    - name: load docker image cache
      shell: |
        cat "{{ item.path }}" | gzip -d -c | docker load
      args: { executable: /bin/bash }
      loop: "{{ docker_images.files }}"
      when: docker_images.files is defined and docker_images.files | length > 0
...
