#  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: Create the kickstart file
  copy:
    src: "{{ role_path }}/files/temp_centos7.ks"
    dest: "{{ role_path }}/files/centos7.ks"
    mode: 0775
  tags: install

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

- 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- Password
  replace:
    path: "{{ role_path }}/files/centos7.ks"
    regexp: '^rootpw --iscrypted password'
    replace: 'rootpw --iscrypted {{ login_pass.stdout }}'
  no_log: true
  tags: install

- name: Configure kickstart file- nic
  lineinfile:
    path: "{{ role_path }}/files/centos7.ks"
    insertafter: '^network  --bootproto=dhcp --device=link --onboot=on --activate'
    line: 'network  --bootproto=dhcp --device={{ item }} --onboot=on --activate'
  tags: install
  with_items: "{{ host_nic }}"

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