validate_hostname.yml 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. ---
  15. - name: Fetch the hostname
  16. command: hostname -s
  17. register: machine_hostname
  18. changed_when: false
  19. - name: Verify the hostname is not blank in hostname
  20. fail:
  21. msg: " {{ hostname_blank_msg }}"
  22. when: machine_hostname.stdout | length < 1
  23. - name: Validate the host name
  24. assert:
  25. that:
  26. - 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])$"))
  27. - machine_hostname.stdout != "localhost"
  28. success_msg: "{{ server_hostname_success }}"
  29. fail_msg: "{{ server_hostname_fail }}"
  30. - name: Fetch the domain name
  31. command: hostname -d
  32. register: domain_name_set
  33. changed_when: false
  34. failed_when: false
  35. - name: Verify the domain name is not blank in hostname
  36. fail:
  37. msg: " {{ domain_name_blank_msg }}"
  38. when: domain_name_set.stdout | length < 1
  39. - name: Set fact for the domain name in hostname
  40. set_fact:
  41. ms_domain_name: "{{ domain_name_set.stdout }}"
  42. - name: Validate the domain name set on the host
  43. assert:
  44. that:
  45. - hostvars['127.0.0.1']['domain_name'] == ms_domain_name
  46. success_msg: "{{ server_domain_name_success }}"
  47. fail_msg: "{{ server_domain_name_fail }}"