- name: init host
  hosts: hosts
  user: root
  any_errors_fatal: true
  vars:
    path: /var/tmp/wise2c/docker
  tasks:
  - name: set hostname
    hostname:
      name: '{{ hostname }}'
    when: format_hostname

  - name: get seed ip
    shell:
      echo $SSH_CONNECTION | cut -d " " -f 1
    register: ip

  - name: add seed to /etc/hosts
    blockinfile:
      path: /etc/hosts
      block: '{{ ip.stdout }} {{ wise2c_seed_host }}'
      marker: '# {mark} WISE2C DEPLOY MANAGED BLOCK {{ wise2c_seed_host }}'

  - name: add to /etc/hosts
    blockinfile:
      path: /etc/hosts
      block: '{{ item.key }} {{ item.value.hostname }}'
      marker: "# {mark} WISE2C DEPLOY MANAGED BLOCK {{ item.key }}"
    with_dict: "{{ hostvars }}"
  
  - name: disabled selinux
    selinux:
      state: disabled

  - name: start firewalld
    systemd:
      name: firewalld
      enabled: true
      state: started

  - name: config firewalld
    shell: |
      firewall-cmd --set-default-zone=trusted
      firewall-cmd --complete-reload

  - name: distribute wise2c repo
    template:
      src: '{{ item.src }}'
      dest: '{{ item.dest }}'
    with_items:
    - { src: 'template/wise2c.repo.j2', dest: '/etc/yum.repos.d/wise2c.repo' }
  
  - name: install docker
    yum:
      disablerepo: '*'
      enablerepo: wise2c
      update_cache: true
      state: present
      name: '{{ item }}'
    with_items:
    - rsync
    - jq
    - docker
    - python-docker-py
    - docker-compose

  - name: distribute chrony server config
    template:
      src: '{{ item.src }}'
      dest: '{{ item.dest }}'
    with_items:
    - { src: 'template/chrony-server.conf.j2', dest: '/etc/chrony.conf' }
    when: inventory_hostname == ansible_play_batch[0]
    
  - name: distribute chrony client config
    template:
      src: '{{ item.src }}'
      dest: '{{ item.dest }}'
    with_items:
    - { src: 'template/chrony-client.conf.j2', dest: '/etc/chrony.conf' }
    when: inventory_hostname != ansible_play_batch[0]

  - name: start chrony
    systemd:
      name: chronyd
      daemon_reload: true
      enabled: true
      state: started

  - name: check docker
    script: scripts/check_docker.sh {{ registry }}
    register: check_output

  - block:    
    - name: clear docker config
      copy:
        content: ''
        dest: '{{ item }}'
      with_items:
      - /etc/sysconfig/docker
      - /etc/sysconfig/docker-storage
      - /etc/sysconfig/docker-storage-setup
      - /etc/sysconfig/docker-network

    - name: distribute docker config
      template:
        src: '{{ item.src }}'
        dest: '{{ item.dest }}'
      with_items:
      - { src: 'template/daemon.json.j2', dest: '/etc/docker/daemon.json' }

    - name: reload & restart docker
      systemd:
        name: docker
        daemon_reload: true
        enabled: true
        state: restarted

    - name: set sysctl
      sysctl:
        name: '{{ item }}'
        value: 1
        state: present
        reload: true
      with_items:
      - net.ipv4.ip_forward
      - net.bridge.bridge-nf-call-iptables
      - net.bridge.bridge-nf-call-ip6tables
    when: check_output.stdout != 'true'
