---
- hosts: localhost
  vars:
    brand_domain: "{{ brand }}.com"
    hostname: "{{ ansible_default_ipv4.address | regex_replace('\\.', '-') }}.{{ brand_domain }}"
    cpanel_ns1: "ns1.{{ hostname }}"
    cpanel_ns2: "ns2.{{ hostname }}"
  connection: local
  tasks:

    - name: Set os_name
      set_fact:
        os_name: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}{{ ansible_distribution_minor_version | default('') }}"

    - name: Setup resolv.conf
      template: src=templates/resolv.conf.j2 dest=/etc/resolv.conf
    - name: Setup yum cron
      template:
        src: templates/yum_cron.j2
        dest: /etc/cron.d/yum

    - name: Random Root Password
      user:
        name: root
        password: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=30 encrypt=sha512_crypt') }}"

    - name: Create .ssh dir
      file:
        path: /root/.ssh
        state: directory
        mode: 0700

    - name: Ensure correct permissions on key
      file:
        path: /root/.ssh/authorized_keys
        mode: 0600
      ignore_errors: yes

    - name: Setup static networking
      template:
        src: templates/ifcfg-eth0.j2
        dest: /etc/sysconfig/network-scripts/ifcfg-eth0
      when:
        - ansible_distribution_major_version == '6'

    - name: Setup static networking
      template:
        src: templates/ifcfg-eth0.j2
        dest: /etc/sysconfig/network-scripts/ifcfg-eth0
      when:
        - ansible_distribution_major_version == '7'

    - name: Setup static networking
      nmcli:
        type: ethernet
        conn_name: ens3
        ip4: "{{ ansible_default_ipv4.address }}"
        gw4: "{{ ansible_default_ipv4.gateway }}"
        state: present

      when:
        - ansible_distribution_major_version == '8'

    - name: set static
      command: nmcli con mod 'ens3' ipv4.method manual ipv4.addresses "{{ ansible_default_ipv4.address }}" gw4 "{{ ansible_default_ipv4.gateway }}"
      when:
        - ansible_distribution_major_version == '8'

    - name: Check for cpanel
      stat: path=/usr/local/cpanel
      register: cpanel

    - name: Call cpanel tasks
      include_tasks: tasks/cpanel.yaml
      when: 
        - cpanel.stat.exists == True

    - name: Call plain tasks
      include_tasks: tasks/plain.yaml
      when: 
        - cpanel.stat.exists == False

    - name: Check for plesk
      stat: path=/usr/local/psa
      register: plesk

    - name: Call plesk tasks
      include_tasks: tasks/plesk.yaml
      when: 
        - plesk.stat.exists == True

    - name: Call brand tasks
      include_tasks: "tasks/{{ brand }}.yaml"
      when: 
        - brand == "hostgator"
        - cpanel.stat.exists == True
        - ansible_distribution_major_version == '7'

    - name: allow outbound ntp traffic in iptables
      iptables:
        chain: OUTPUT
        protocol: udp
        destination_port: 123
        comment: "Outbound 123/udp for NTP"
        jump: ACCEPT

    - name: remove bandmin crons
      lineinfile:
        dest: /var/spool/cron/root
        regexp: '(.*)bandmin(.*)'
        state: absent
      ignore_errors: yes

    - name: Indicate done
      file:
        path: /root/.bootstrapped
        state: touch
