create_inventory.yml 8.2 KB

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