create_inventory.yml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright 2021 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: Get service tag
  155. shell: >
  156. set -o pipefail && \
  157. dmidecode -t 1 | grep Serial
  158. changed_when: false
  159. failed_when: false
  160. register: service_tag_details
  161. when: hostname_check.stdout is defined
  162. - name: Set fact service tag
  163. set_fact:
  164. service_tag: "{{ service_tag_details.stdout.split(':')[1].strip() }}"
  165. when: service_tag_details.stdout is defined
  166. - name: Update inventory
  167. hosts: reachable
  168. connection: local
  169. gather_facts: false
  170. tasks:
  171. - name: Encrypt omnia_config.yml file
  172. command: >-
  173. ansible-vault encrypt "{{ hostvars['localhost']['omnia_config_file'] }}"
  174. --vault-password-file "{{ hostvars['localhost']['omnia_config_vault_file'] }}"
  175. changed_when: false
  176. delegate_to: localhost
  177. run_once: true
  178. - name: Update omnia_config.yml permissions
  179. file:
  180. path: "{{ hostvars['localhost']['omnia_config_file'] }}"
  181. mode: "{{ hostvars['localhost']['file_perm'] }}"
  182. delegate_to: localhost
  183. run_once: true
  184. - name: Check if tower_config_file file is encrypted
  185. command: cat "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  186. changed_when: false
  187. no_log: true
  188. register: tower_config_content
  189. delegate_to: localhost
  190. run_once: true
  191. - name: Decrypt tower_config_file
  192. command: >-
  193. ansible-vault decrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  194. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  195. changed_when: false
  196. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  197. delegate_to: localhost
  198. run_once: true
  199. - name: Change file permissions - tower_config_file
  200. file:
  201. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  202. mode: "{{ hostvars['localhost']['file_perm'] }}"
  203. delegate_to: localhost
  204. run_once: true
  205. - name: Fetch awx host
  206. command: grep "host:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  207. register: fetch_awx_host
  208. changed_when: false
  209. delegate_to: localhost
  210. run_once: true
  211. - name: Fetch awx username
  212. command: grep "username:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  213. register: fetch_awx_username
  214. changed_when: false
  215. run_once: true
  216. delegate_to: localhost
  217. run_once: true
  218. - name: Fetch awx password
  219. command: grep "password:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  220. register: fetch_awx_password
  221. changed_when: false
  222. run_once: true
  223. delegate_to: localhost
  224. run_once: true
  225. - name: Set awx variables
  226. set_fact:
  227. awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
  228. awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
  229. awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
  230. no_log: true
  231. - name: Encrypt tower_config_file
  232. command: >-
  233. ansible-vault encrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  234. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  235. changed_when: false
  236. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  237. run_once: true
  238. delegate_to: localhost
  239. run_once: true
  240. - name: Change file permissions - tower_config_file
  241. file:
  242. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  243. mode: "{{ hostvars['localhost']['file_perm'] }}"
  244. delegate_to: localhost
  245. run_once: true
  246. - name: Fetch facts and add new hosts
  247. include_tasks: add_host.yml