---
  - name: Unistall firewalld on OpenSuse Leap 15
    zypper: name=firewalld state=absent
    when: ansible_os_family == "Suse" and ansible_distribution_major_version == "15"

  - name: Check if configuration already exists
    stat: path=/etc/re6stnet/re6stnet.conf
    register: re6stnet_conf

  - include: sysctl.yml

  - name: Update Facts
    set_fact:
      location_option: "--location {{ location }}"
    when: "{{ location | length | int }} > 0"

  - name: Update Facts
    set_fact:
      location_option: ""
    when: "{{ location | length | int }} == 0"

  - name: Configure Re6st with re6st-conf
    shell: "re6st-conf --registry {{ re6st_registry_url }} -r title {{ computer_name }} --fingerprint {{ re6st_fingerprint }} -d /etc/re6stnet --anonymous {{ location_option }}"
    when: re6st_annon == True and re6stnet_conf.stat.exists == False and "{{ computer_name }}" != "noname"

  - name: Update Facts
    set_fact: 
      computer_name: "{{ re6sttoken }}"
    when: computer_name  == "noname"

  - debug: msg="{{ computer_name }}"


  - name: Configure Re6st with re6st-conf
    shell: "re6st-conf --registry {{ re6st_registry_url }} --token {{ re6sttoken }} -r title {{ computer_name }} --fingerprint {{ re6st_fingerprint }} -d /etc/re6stnet {{ location_option }}"
    when: re6st_annon == False and re6stnet_conf.stat.exists == False and "{{ re6sttoken }}" != "notoken" and "{{ re6st_fingerprint }}" != "nofingerprint"

  - name: Configure Re6st with re6st-conf
    shell: "re6st-conf --registry {{ re6st_registry_url }} --token {{ re6sttoken }} -r title {{ re6sttoken }} -d /etc/re6stnet {{ location_option }}"
    when: re6st_annon == False and re6stnet_conf.stat.exists == False and "{{ re6sttoken }}" != "notoken" and "{{ re6st_fingerprint }}" == "nofingerprint"

  - name: Recheck if configuration already exists (after running re6st-conf)
    stat: path=/etc/re6stnet/re6stnet.conf
    register: recheck_re6stnet_conf

  - name: stat re6stnet.service
    stat: path=/etc/systemd/re6stnet.service
    register: re6stnet
  
  - name: stat re6st-node.service
    stat: path=/etc/systemd/system/re6st-node.service
    register: re6stnode
  
  - name: set re6st-node.service file
    copy: src=/etc/systemd/re6stnet.service dest=/etc/systemd/system/re6stnet.service
    when: ansible_distribution == 'CentOS' and ansible_distribution_version >= '7' and re6stnet.stat.exists == True and re6stnode.stat.exists == False

  - include: daemon.yml
    
  - name: Create centos 6 init.d missing file
    copy: src=centos_6_init_d dest=/etc/init.d/re6stnet mode=755
    when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == '6' and recheck_re6stnet_conf.stat.exists == True

  - name: Add script for check ip6tables well configured
    copy: src=ip6tables dest=/usr/bin/re6stnet-ip6tables-check mode=755

  - name: Include re6stnet-ip6tables-check at reboot on cron
    cron: name="ip6tables at reboot" special_time=reboot job="sleep 20 && /usr/bin/re6stnet-ip6tables-check"

  - include: tcp_nodelay.yml

