# Copyright 2022 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: Verify domain name in hostname
  block:
    - name: Fetch the hostname
      command: hostname -s
      register: machine_hostname
      changed_when: false

    - name: Verify the hostname is not blank in hostname
      fail:
        msg: " {{ hostname_blank_msg }}"
      when: machine_hostname.stdout | length < 1

    - name: Validate the host name
      assert:
        that:
          - machine_hostname.stdout is regex(("^(([a-z]|[a-z][a-z0-9\-]*[a-z0-9])\.)*([a-z]|[a-z][a-z0-9\-]*[a-z0-9])$"))
          - machine_hostname.stdout != "localhost"
        success_msg: "{{ server_hostname_success }}"
        fail_msg: "{{ server_hostname_fail }}"

    - name: Fetch the domain name
      command: hostname -d
      register: domain_name_set
      changed_when: false

    - name: Verify the domain name is not blank in hostname
      fail:
        msg: " {{ domain_name_blank_msg }}"
      when: domain_name_set.stdout | length < 1

    - name: Set fact for the domain name in hostname
      set_fact:
        ms_domain_name: "{{ domain_name_set.stdout }}"

    - name: Validate the domain name set on the host
      assert:
        that:
          - hostvars['127.0.0.1']['domain_name'] == ms_domain_name
        success_msg: "{{ server_domain_name_success }}"
        fail_msg: "{{ server_domain_name_fail }}"
  when:
    - hostvars['127.0.0.1']['login_node_required']
    - hostvars['127.0.0.1']['ipa_server_ms'] and login_node_group in group_names or
      not hostvars['127.0.0.1']['ipa_server_ms']