mapping_file.yml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2020 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. # limitations under the License.
  13. ---
  14. - name: Check if file is comma seperated
  15. shell: awk -F\, '{print NF-1}' {{ path_for_mapping_file }}
  16. register: comma_seperated
  17. changed_when: false
  18. tags: install
  19. - name: Fail if not comma seperated
  20. fail:
  21. msg: "{{ not_comma_seperated }}"
  22. when: item != "2"
  23. with_items: "{{ comma_seperated.stdout_lines }}"
  24. tags: install
  25. - name: Remove blank lines
  26. shell: awk -F, 'length>NF+1' {{ path_for_mapping_file }} > {{ role_path }}/files/new_mapping_file.csv
  27. changed_when: false
  28. tags: install
  29. - name: Remove blank spaces
  30. shell: sed -i.bak -E 's/(^|,)[[:blank:]]+/\1/g; s/[[:blank:]]+(,|$)/\1/g' {{ role_path }}/files/new_mapping_file.csv
  31. args:
  32. warn: no
  33. changed_when: false
  34. tags: install
  35. - name: Check if header present
  36. shell: awk 'NR==1 { print $1}' {{ role_path }}/files/new_mapping_file.csv
  37. register: header
  38. changed_when: false
  39. tags: install
  40. - name: Fail if header not present
  41. fail:
  42. msg: "{{ header_fail }}"
  43. when: header.stdout != valid_header
  44. - name: Count the hostname
  45. shell: awk -F',' '{print $2}' {{ role_path }}/files/new_mapping_file.csv | wc -l
  46. register: total_hostname
  47. changed_when: false
  48. tags: install
  49. - name: Count the ip
  50. shell: awk -F',' '{print $3}' {{ role_path }}/files/new_mapping_file.csv | wc -l
  51. register: total_ip
  52. changed_when: false
  53. tags: install
  54. - name: Count the macs
  55. shell: awk -F',' '{print $1}' {{ role_path }}/files/new_mapping_file.csv | wc -l
  56. register: total_mac
  57. changed_when: false
  58. tags: install
  59. - name: Check for duplicate hostname
  60. shell: awk -F',' '{print $2}' {{ role_path }}/files/new_mapping_file.csv | uniq | wc -l
  61. register: uniq_hostname
  62. changed_when: false
  63. tags: install
  64. - name: Check for duplicate ip
  65. shell: awk -F',' '{print $3}' {{ role_path }}/files/new_mapping_file.csv | uniq | wc -l
  66. register: uniq_ip
  67. changed_when: false
  68. tags: install
  69. - name: Check for duplicate mac
  70. shell: awk -F',' '{print $1}' {{ role_path }}/files/new_mapping_file.csv | uniq | wc -l
  71. register: uniq_mac
  72. changed_when: false
  73. tags: install
  74. - name: Fail if duplicate hosts exist
  75. fail:
  76. msg: "{{ fail_hostname_duplicate }}"
  77. when: total_hostname.stdout > uniq_hostname.stdout
  78. tags: install
  79. - name: Fail if duplicate ips exist
  80. fail:
  81. msg: "{{ fail_ip_duplicate }}"
  82. when: total_ip.stdout > uniq_ip.stdout
  83. tags: install
  84. - name: Fail if duplicate mac exist
  85. fail:
  86. msg: "{{ fail_mac_duplicate }}"
  87. when: total_mac.stdout > uniq_mac.stdout
  88. tags: install
  89. - name: Check if _ or . or space present in hostname
  90. shell: awk -F',' '{print $2}' {{ role_path }}/files/new_mapping_file.csv |grep -E -- '_|\.| '
  91. register: hostname_result
  92. ignore_errors: true
  93. changed_when: false
  94. tags: install
  95. - name: Fail if _ or . or space present in hostname
  96. fail:
  97. msg: "{{ hostname_result.stdout + ' :Hostname should not contain _ or . as it will cause error with slurm and K8s'}}"
  98. when: hostname_result.stdout != ""
  99. tags: install
  100. - name: Compare the file for new nodes
  101. block:
  102. - name: difference
  103. shell: diff {{ role_path }}/files/new_mapping_file.csv {{role_path}}/files/backup_mapping_file.csv| tr -d \>|tr -d \<| grep -E -- ', & :| '
  104. register: diff_output
  105. when: backup_map_status == true
  106. - name: status of new nodes
  107. set_fact:
  108. new_node_status: true
  109. when: diff_output.stdout!= ""
  110. rescue:
  111. - name: No new nodes
  112. debug:
  113. msg: "No new nodes to add"
  114. verbosity: 2
  115. - name: Fetch input
  116. blockinfile:
  117. path: "{{ role_path }}/files/dhcp.template"
  118. insertafter: '^#insert the static DHCP leases for configuration here'
  119. block: |
  120. host {{ item.split(',')[1] }} {
  121. hardware ethernet {{ item.split(',')[0] }};
  122. fixed-address {{ item.split(',')[2] }};
  123. }
  124. marker: "# {mark} DHCP BLOCK OF {{ item.split(',')[0] }}"
  125. with_lines: "{{ remove_header }}"
  126. ignore_errors: true
  127. when: (not cobbler_image_status) or (new_node_status == true)
  128. tags: install
  129. - name: Create a backup file
  130. copy:
  131. src: "{{ role_path }}/files/new_mapping_file.csv"
  132. dest: "{{ role_path }}/files/backup_mapping_file.csv"
  133. - name: Copy the dhcp.template inside container
  134. command: docker exec cobbler cp /root/omnia/appliance/roles/provision/files/dhcp.template /etc/cobbler/dhcp.template
  135. when: ( cobbler_container_status == true ) and ( new_node_status == true )
  136. - name: Cobbler sync for adding new nodes
  137. command: docker exec cobbler cobbler sync
  138. when: ( cobbler_container_status == true ) and ( new_node_status == true )
  139. - name: Restart dhcpd
  140. command: docker exec cobbler systemctl restart dhcpd
  141. when: ( cobbler_container_status == true ) and ( new_node_status == true )