  - name: Ensure software directory exists
    file:
      path: /opt/slapgrid
      state: directory
      mode: 0755

  - name: Ensure instance directory exists
    file:
      path: /srv/slapgrid
      state: directory
      mode: 0755

  - name: Install packages using apt
    apt: name=slapos-node state=present update_cache=yes cache_valid_time=3600
    when: ansible_os_family == "Debian" and not slapos_ansible_offline

  - name: Uninstall firewalld on CentOS
    yum: name=firewalld state=absent update_cache=yes
    when: ansible_distribution == "CentOS"

  - name: Install re6stnet on CentOS
    yum: name=slapos.node state=present update_cache=yes
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version != "7" and not slapos_ansible_offline

  - name: Download slapos.node from Shacache for CentOS 7
    get_url:
      url: http://download.shacache.org/2ad0622eff0aa0ec3a0e8d872ca948013d216d8fc837ef0f9919290afc9372319881715764987350302172022e288d4d58f31f71980071d652d66d293533da7b
      dest: /tmp/slapos.node-1.0.56-2.1.x86_64.rpm
      checksum: "md5:37446cc16b38fa5769f3e084294c8683"
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" and not slapos_ansible_offline

  - name: Install re6stnet on CentOS 7
    yum: name=/tmp/slapos.node-1.0.56-2.1.x86_64.rpm state=present update_cache=yes
    when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" and not slapos_ansible_offline

  - name: Check if configuration exists already
    stat: path=/etc/opt/slapos/slapos.cfg
    register: slapos_cfg

  - name: Configure SlapOS with slapos configure local
    shell: "slapos configure local"
    when: slapos_cfg.stat.exists == False 

  - name: Disable multimaster
    shell: sed -i '/# Here goes the list of/,/\/some\/arbitrary\/local\/unix\/path/d' /etc/opt/slapos/slapos-proxy.cfg
    ignore_errors: True

  - name: Wait for proxy
    wait_for: host=127.0.0.1 port=8080 delay=10

  - name: Add ipv6 to lo interface
    shell: ip -6 addr add fd46::1/64 dev lo
    ignore_errors: True

  - name: Run slapos format for initial bootstrap
    shell: "PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/slapos/bin slapos node format --now --alter_user=True"

  - name: Set ownership for /opt/slapgrid
    shell: "chown slapsoft:slapsoft /opt/slapgrid"

  - name: Disable "slapos node boot"
    lineinfile: dest="/etc/cron.d/slapos-node" regexp="^@reboot root /opt/slapos/bin/slapos node boot" state=absent
