create_inventory.yml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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: all
  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: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and ( mapping_file | bool == true )
  88. register: host_name
  89. ignore_errors: true
  90. - name: Set the hostname from mapping file
  91. hostname:
  92. name: "{{ host_name.stdout + '.' + hostvars['localhost']['domain_name'] }}"
  93. when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and (mapping_file | bool == true )
  94. ignore_errors: true
  95. - name: Set the hostname if hostname not present mapping file
  96. hostname:
  97. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] + '.' + hostvars['localhost']['domain_name'] }}"
  98. when: ('localhost' in hostname_check.stdout) and (file_present.rc != 0) and (mapping_file | bool == true )
  99. ignore_errors: true
  100. - name: Set the system hostname
  101. hostname:
  102. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1]+'.'+ hostvars['localhost']['domain_name'] }}"
  103. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false)
  104. ignore_errors: true
  105. - name: Add new hostname to /etc/hosts from mapping file
  106. lineinfile:
  107. dest: /etc/hosts
  108. line: "{{ inventory_hostname }} {{ host_name.stdout + '.' + hostvars['localhost']['domain_name'] }}"
  109. state: present
  110. when: ('localhost' in hostname_check.stdout) and ( mapping_file_present != "" ) and ( mapping_file | bool == true )
  111. ignore_errors: true
  112. - name: Add new hostname to /etc/hosts if hostname not present mapping file
  113. lineinfile:
  114. dest: /etc/hosts
  115. line: "{{ inventory_hostname }} compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1]+'.'+ hostvars['localhost']['domain_name'] }}"
  116. state: present
  117. when: ('localhost' in hostname_check.stdout) and ( file_present.rc != 0 ) and ( mapping_file | bool == true )
  118. ignore_errors: true
  119. - name: Add new hostname to /etc/hosts
  120. lineinfile:
  121. dest: /etc/hosts
  122. line: "{{ inventory_hostname }} compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] +'.'+ hostvars['localhost']['domain_name'] }}"
  123. state: present
  124. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false )
  125. ignore_errors: true
  126. - name: Update inventory
  127. hosts: localhost
  128. connection: local
  129. gather_facts: false
  130. tasks:
  131. - name: Encrypt omnia_config.yml file
  132. command: >-
  133. ansible-vault encrypt "{{ omnia_config_file }}"
  134. --vault-password-file "{{ omnia_config_vault_file }}"
  135. changed_when: false
  136. - name: Update omnia_config.yml permissions
  137. file:
  138. path: "{{ omnia_config_file }}"
  139. mode: "{{ file_perm }}"
  140. - name: Check if tower_config_file file is encrypted
  141. command: cat "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  142. changed_when: false
  143. no_log: true
  144. register: tower_config_content
  145. run_once: true
  146. - name: Decrypt tower_config_file
  147. command: >-
  148. ansible-vault decrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  149. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  150. changed_when: false
  151. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  152. run_once: true
  153. - name: Change file permissions
  154. file:
  155. path: "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  156. mode: "{{ file_perm }}"
  157. - name: Fetch awx host
  158. command: grep "host:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  159. register: fetch_awx_host
  160. changed_when: false
  161. run_once: true
  162. - name: Fetch awx username
  163. command: grep "username:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  164. register: fetch_awx_username
  165. changed_when: false
  166. run_once: true
  167. no_log: true
  168. - name: Fetch awx password
  169. command: grep "password:" "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  170. register: fetch_awx_password
  171. changed_when: false
  172. run_once: true
  173. no_log: true
  174. - name: Set awx variables
  175. set_fact:
  176. awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
  177. awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
  178. awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
  179. no_log: true
  180. - name: Encrypt tower_config_file
  181. command: >-
  182. ansible-vault encrypt "{{ playbook_dir }}/../../webui_awx/files/.tower_cli.cfg"
  183. --vault-password-file "{{ playbook_dir }}/../../webui_awx/files/.tower_vault_key"
  184. changed_when: false
  185. when: "'$ANSIBLE_VAULT;' in tower_config_content.stdout"
  186. run_once: true
  187. - name: Update inventory file
  188. block:
  189. - name: Fetch facts and add new hosts
  190. include_tasks: add_host.yml
  191. with_items: "{{ groups['reachable'] }}"
  192. when: "'reachable' in groups"
  193. - name: Show unreachable hosts
  194. debug:
  195. msg: "{{ host_unreachable_msg }} + {{ groups['ungrouped'] }}"
  196. when: "'ungrouped' in groups"