---
- hosts: nodes
  become: yes
  vars_files:
    - vagrant.yml

  tasks:
    - name: clean up the home folder
      file:
        path: /home/vagrant/{{ item }}
        state: absent
      with_items:
        - base.sh
        - chef.sh
        - cleanup.sh
        - cleanup-virtualbox.sh
        - puppetlabs-release-wheezy.deb
        - puppet.sh
        - ruby.sh
        - vagrant.sh
        - virtualbox.sh
        - zerodisk.sh

    - name: installing dependencies
      apt:
        name: apt-transport-https,ca-certificates,python3-pip,tmux
        state: present
        update_cache: true

    - name: fetching docker repo key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: adding docker repo
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: installing docker
      apt:
        name: docker-ce,docker-ce-cli,containerd.io,docker-compose-plugin
        state: present
        update_cache: true

    - name: adding user vagrant to group docker
      user:
        name: vagrant
        groups: docker
        append: yes

    - name: making docker daemon listen to port 55555
      lineinfile:
        dest: /etc/default/docker
        line: DOCKER_OPTS="--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:55555"
        regexp: "^#?DOCKER_OPTS=.*$"
        state: present
      register: docker_opts

    - name: restarting docker daemon, if needed
      service:
        name: docker
        state: restarted
      when: docker_opts is defined and docker_opts.changed

    - name: install docker-compose from official github repo
      get_url:
        url: https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64
        dest: /usr/local/bin/docker-compose
        mode: "u+x,g+x"

    - name:
      file: path="/usr/local/bin/docker-compose"
        state=file
        mode=0755
        owner=vagrant
        group=docker

    - name: building the /etc/hosts file with all nodes
      lineinfile:
        dest: /etc/hosts
        line: "{{ item.private_ip }} {{ item.hostname }}"
        regexp: "^{{ item.private_ip }} {{ item.hostname }}$"
        state: present
      with_items: "{{ instances }}"

    - name: copying the ssh key to the nodes
      copy:
        src: private-key
        dest: /home/vagrant/private-key
        mode: 0600
        group: root
        owner: vagrant

    - name: copying ssh configuration
      copy:
        src: ssh-config
        dest: /home/vagrant/.ssh/config
        mode: 0600
        group: root
        owner: vagrant

    - name: fixing the hostname
      hostname:
        name: "{{ inventory_hostname }}"

    - name: adjusting the /etc/hosts to the new hostname
      lineinfile:
        dest: /etc/hosts
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
        owner: root
        group: root
        mode: 0644
      with_items:
        - regexp: '^127\.0\.0\.1'
          line: "127.0.0.1 localhost {{ inventory_hostname }}"
        - regexp: '^127\.0\.1\.1'
          line: "127.0.1.1 {{ inventory_hostname }}"
