#  Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---

- name: Remove old user
  file:
    path: "{{ role_path }}/files/.users.digest"
    state: absent
  tags: install

- name: Create a new user
  file:
    path: "{{ role_path }}/files/.users.digest"
    state: touch
    mode: "{{ user_mode }}"
  tags: install

- name: Cobbler UI password
  set_fact:
        encrypt_password: "{{ cobbler_password | hash('sha3_256') }}"
  no_log: true
  tags: install

- name: Copy cobbler password to cobbler config file
  shell: printf "%s:%s:%s\n" "{{ username }}" "Cobbler" "{{ encrypt_password }}" > "{{ role_path }}/files/.users.digest"
  changed_when: false
  no_log: true
  tags: install

- name: Kickstart configuration - centos
  block:
    - name: Create the kickstart file
      copy:
        src: "{{ role_path }}/files/temp_centos7.ks"
        dest: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        mode: 0775
      tags: install

    - name: Configure kickstart file - IP
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^url --url http://ip/cblr/links/centos-x86_64/'
        replace: url --url http://{{ hpc_ip }}/cblr/links/centos-x86_64/
      tags: install

    - name: Configure kickstart file - nic
      lineinfile:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        insertafter: '^network  --bootproto=dhcp --device=link --onboot=on --activate'
        line: 'network  --bootproto=dhcp --device={{ item }} --onboot=on --activate'
      tags: install
      with_items: "{{ centos_host_nic }}"
  when: provision_os == os_supported_centos

- name: Kickstart configuration - rocky
  block:
    - name: Create the kickstart file
      copy:
        src: "{{ role_path }}/files/temp_rocky8.ks"
        dest: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        mode: 0775
      tags: install

    - name: Configure kickstart file - IP
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^url --url http://ip/cblr/links/rocky-x86_64/'
        replace: url --url http://{{ hpc_ip }}/cblr/links/rocky-x86_64/
      tags: install

    - name: Configure kickstart file - nic
      lineinfile:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        insertafter: '^network  --bootproto=dhcp --device=link --onboot=on --activate'
        line: 'network  --bootproto=dhcp --device={{ item }} --onboot=on --activate'
      tags: install
      with_items: "{{ rocky_host_nic }}"
  when: provision_os == os_supported_rocky

- name: Kickstart configuration - leap
  block:
    - name: Create the kickstart file
      copy:
        src: "{{ role_path }}/files/temp_leap15.xml"
        dest: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        mode: 0775
      tags: install

    - name: Configure kickstart file - IP
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^          install: http://ip/cblr/links/leap-x86_64/'
        replace: '          install: http://{{ hpc_ip }}/cblr/links/leap-x86_64/'
      tags: install
  when: provision_os == os_supported_leap

- name: Random phrase generation
  command: openssl rand -base64 12
  changed_when: false
  register: prompt_random_phrase
  tags: install
  no_log: true

- name: Set random phrase
  set_fact:
    random_phrase: "{{ prompt_random_phrase.stdout }}"
  tags: install
  no_log: true

- name: Login password
  command: openssl passwd -1 -salt {{ random_phrase }} {{ provision_password }}
  no_log: true
  changed_when: false
  register: login_pass
  tags: install

- name: Assign password
  replace:
    path: "{{ role_path }}/files/settings.yaml"
    regexp: '^default_password_crypted: password'
    replace: 'default_password_crypted: {{ login_pass.stdout }}'
  no_log: true
  tags: install

- name: Configure kickstart file for {{ provision_os }}
  block:
    - name: Configure kickstart file - Password
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^rootpw --iscrypted ks_password'
        replace: 'rootpw --iscrypted {{ login_pass.stdout }}'
      no_log: true
      tags: install

    - name: Configure kickstart file - timezone
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^timezone --utc ks_timezone'
        replace: 'timezone --utc {{ timezone }}'
      tags: install

    - name: Configure kickstart file - language
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^lang ks_language'
        replace: 'lang {{ language }}'
      tags: install
  when: 
    - provision_os != os_supported_leap

- name: Configure kickstart file for {{ provision_os }}
  block:
    - name: Configure kickstart file - Password
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^      <user_password>ks_password</user_password>'
        replace: '      <user_password>{{ login_pass.stdout }}</user_password>'
      no_log: true
      tags: install

    - name: Configure kickstart file - timezone
      replace:
        path: "{{ role_path }}/files/{{ cobbler_kickstart_file }}"
        regexp: '^    <timezone>ks_timezone</timezone>'
        replace: '    <timezone>{{ timezone }}</timezone>'
      tags: install
  when: provision_os == os_supported_leap

- name: Remove ^M characters
  command: dos2unix {{ role_path }}/files/{{ cobbler_kickstart_file }}
  changed_when: false
  failed_when: false