create_inventory.yml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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: Find reachable hosts
  16. hosts: node_inventory
  17. gather_facts: false
  18. ignore_unreachable: true
  19. ignore_errors: true
  20. tasks:
  21. - name: Check for reachable nodes
  22. command: ping -c1 {{ inventory_hostname }}
  23. delegate_to: localhost
  24. register: ping_result
  25. ignore_errors: yes
  26. changed_when: false
  27. - name: Refresh ssh keys
  28. command: ssh-keygen -R {{ inventory_hostname }}
  29. delegate_to: localhost
  30. changed_when: false
  31. - name: Group reachable hosts
  32. group_by:
  33. key: "reachable"
  34. when: "'100% packet loss' not in ping_result.stdout"
  35. - name: Get provision password
  36. hosts: localhost
  37. connection: local
  38. gather_facts: false
  39. tasks:
  40. - name: Include vars file of inventory role
  41. include_vars: ../vars/main.yml
  42. - name: Check if omnia config file is encrypted
  43. command: "cat {{ omnia_config_file }}"
  44. changed_when: false
  45. register: config_content
  46. no_log: true
  47. - name: Decrpyt omnia_config.yml
  48. command: >-
  49. ansible-vault decrypt "{{ omnia_config_file }}"
  50. --vault-password-file "{{ omnia_config_vault_file }}"
  51. when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  52. - name: Include vars file of inventory role
  53. include_vars: "{{ omnia_config_file }}"
  54. - name: Set hostname for reachable nodes and gather facts
  55. hosts: reachable
  56. gather_facts: False
  57. ignore_unreachable: true
  58. remote_user: "{{ host_username }}"
  59. vars:
  60. ansible_password: "{{ host_password }}"
  61. ansible_become_pass: "{{ host_password }}"
  62. ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  63. mapping_file_present: ""
  64. tasks:
  65. - name: Setup
  66. setup:
  67. filter: ansible_*
  68. - name: Check hostname of server
  69. command: hostname
  70. register: hostname_check
  71. changed_when: false
  72. ignore_errors: true
  73. - name: Check if IP is present in mapping file
  74. command: grep "{{ inventory_hostname }}" ../../provision_cobbler/files/new_host_mapping_file.csv
  75. delegate_to: localhost
  76. register: file_present
  77. when: mapping_file | bool == true
  78. ignore_errors: true
  79. - name: Set fact if mapping file is present
  80. set_fact:
  81. mapping_file_present: "{{ file_present.stdout }}"
  82. when: mapping_file | bool == true
  83. ignore_errors: true
  84. - name: Get the static hostname from mapping file
  85. shell: awk -F',' '$3 == "{{ inventory_hostname }}" { print $2 }' ../../provision_cobbler/files/new_host_mapping_file.csv
  86. delegate_to: localhost
  87. when:
  88. - ( hostname_check.stdout is defined )
  89. - ( 'localhost' in hostname_check.stdout )
  90. - ( mapping_file_present != "" )
  91. - ( mapping_file | bool == true )
  92. register: host_name
  93. ignore_errors: true
  94. - name: Set the hostname from mapping file
  95. command: hostnamectl set-hostname "{{ host_name.stdout + '.' + hostvars['localhost']['domain_name'] }}"
  96. when:
  97. - ( hostname_check.stdout is defined )
  98. - ( 'localhost' in hostname_check.stdout )
  99. - ( mapping_file_present != "" )
  100. - ( mapping_file | bool == true )
  101. ignore_errors: true
  102. - name: Set the hostname if hostname not present mapping file
  103. command: hostnamectl set-hostname "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] + '.' + hostvars['localhost']['domain_name'] }}"
  104. when:
  105. - ( hostname_check.stdout is defined )
  106. - ( 'localhost' in hostname_check.stdout )
  107. - ( file_present.rc is defined )
  108. - ( file_present.rc != 0 )
  109. - ( mapping_file | bool == true )
  110. ignore_errors: true
  111. - name: Set the system hostname if mapping file not present
  112. command: hostnamectl set-hostname "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1]+'.'+ hostvars['localhost']['domain_name'] }}"
  113. when:
  114. - ( hostname_check.stdout is defined )
  115. - ( 'localhost' in hostname_check.stdout )
  116. - ( mapping_file | bool == false )
  117. ignore_errors: true
  118. - name: Add new hostname to /etc/hosts from mapping file
  119. lineinfile:
  120. dest: /etc/hosts
  121. line: "{{ inventory_hostname }} {{ host_name.stdout + '.' + hostvars['localhost']['domain_name'] }}"
  122. state: present
  123. when:
  124. - ( hostname_check.stdout is defined )
  125. - ( 'localhost' in hostname_check.stdout )
  126. - ( mapping_file_present != "" )
  127. - ( mapping_file | bool == true )
  128. ignore_errors: true
  129. - name: Add new hostname to /etc/hosts if hostname not present mapping file
  130. lineinfile:
  131. dest: /etc/hosts
  132. line: "{{ inventory_hostname }} compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1]+'.'+ hostvars['localhost']['domain_name'] }}"
  133. state: present
  134. when:
  135. - ( hostname_check.stdout is defined )
  136. - ( 'localhost' in hostname_check.stdout )
  137. - ( file_present.rc is defined )
  138. - ( file_present.rc != 0 )
  139. - ( mapping_file | bool == true )
  140. ignore_errors: true
  141. - name: Add new hostname to /etc/hosts if mapping file not present
  142. lineinfile:
  143. dest: /etc/hosts
  144. line: "{{ inventory_hostname }} compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] +'.'+ hostvars['localhost']['domain_name'] }}"
  145. state: present
  146. when:
  147. - ( hostname_check.stdout is defined )
  148. - ( 'localhost' in hostname_check.stdout )
  149. - ( mapping_file | bool == false )
  150. ignore_errors: true
  151. - name: Initialize service tag
  152. set_fact:
  153. service_tag: "Not Found"
  154. - name: Install dmidecode package
  155. package:
  156. name: dmidecode
  157. state: present
  158. - name: Get service tag
  159. shell: >
  160. set -o pipefail && \
  161. dmidecode -t 1 | grep Serial
  162. changed_when: false
  163. failed_when: false
  164. register: service_tag_details
  165. when: hostname_check.stdout is defined
  166. - name: Set fact service tag
  167. set_fact:
  168. service_tag: "{{ service_tag_details.stdout.split(':')[1].strip() }}"
  169. when: service_tag_details.stdout is defined
  170. - name: Update inventory
  171. hosts: reachable
  172. connection: local
  173. gather_facts: false
  174. tasks:
  175. - name: Encrypt omnia_config.yml file
  176. command: >-
  177. ansible-vault encrypt "{{ hostvars['localhost']['omnia_config_file'] }}"
  178. --vault-password-file "{{ hostvars['localhost']['omnia_config_vault_file'] }}"
  179. changed_when: false
  180. delegate_to: localhost
  181. run_once: true
  182. - name: Update omnia_config.yml permissions
  183. file:
  184. path: "{{ hostvars['localhost']['omnia_config_file'] }}"
  185. mode: "{{ hostvars['localhost']['file_perm'] }}"
  186. delegate_to: localhost
  187. run_once: true
  188. - name: Check if tower_config_file file is encrypted
  189. command: cat "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  190. changed_when: false
  191. no_log: true
  192. register: tower_config_content
  193. delegate_to: localhost
  194. run_once: true
  195. - name: Decrypt tower_config_file
  196. command: >-
  197. ansible-vault decrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  198. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  199. changed_when: false
  200. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  201. delegate_to: localhost
  202. run_once: true
  203. - name: Change file permissions - tower_config_file
  204. file:
  205. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  206. mode: "{{ hostvars['localhost']['file_perm'] }}"
  207. delegate_to: localhost
  208. run_once: true
  209. - name: Fetch awx host
  210. command: grep "host:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  211. register: fetch_awx_host
  212. changed_when: false
  213. delegate_to: localhost
  214. run_once: true
  215. - name: Fetch awx username
  216. command: grep "username:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  217. register: fetch_awx_username
  218. changed_when: false
  219. run_once: true
  220. delegate_to: localhost
  221. run_once: true
  222. - name: Fetch awx password
  223. command: grep "password:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  224. register: fetch_awx_password
  225. changed_when: false
  226. run_once: true
  227. delegate_to: localhost
  228. run_once: true
  229. - name: Set awx variables
  230. set_fact:
  231. awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
  232. awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
  233. awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
  234. no_log: true
  235. - name: Encrypt tower_config_file
  236. command: >-
  237. ansible-vault encrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  238. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  239. changed_when: false
  240. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  241. run_once: true
  242. delegate_to: localhost
  243. run_once: true
  244. - name: Change file permissions - tower_config_file
  245. file:
  246. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  247. mode: "{{ hostvars['localhost']['file_perm'] }}"
  248. delegate_to: localhost
  249. run_once: true
  250. - name: Fetch facts and add new hosts
  251. include_tasks: add_host.yml