create_inventory.yml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # 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: Set hostname on reachable nodes and gather facts
  43. hosts: reachable
  44. gather_facts: False
  45. ignore_unreachable: true
  46. remote_user: "{{ cobbler_username }}"
  47. vars:
  48. ansible_password: "{{ cobbler_password }}"
  49. ansible_become_pass: "{{ cobbler_password }}"
  50. ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  51. mapping_file_present: ""
  52. tasks:
  53. - name: Setup
  54. setup:
  55. filter: ansible_*
  56. - name: Check hostname of server
  57. command: hostname
  58. register: hostname_check
  59. changed_when: false
  60. ignore_errors: true
  61. - name: Check if IP present in mapping file
  62. command: grep "{{ inventory_hostname }}" ../../provision/files/new_mapping_file.csv
  63. delegate_to: localhost
  64. register: file_present
  65. when: mapping_file | bool == true
  66. ignore_errors: true
  67. - name: Set fact if mapping file present
  68. set_fact:
  69. mapping_file_present: "{{ file_present.stdout }}"
  70. when: mapping_file | bool == true
  71. ignore_errors: true
  72. - name: Get the static hostname from mapping file
  73. shell: awk -F',' '$3 == "{{ inventory_hostname }}" { print $2 }' ../../provision/files/new_mapping_file.csv
  74. delegate_to: localhost
  75. when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and ( mapping_file | bool == true )
  76. register: host_name
  77. ignore_errors: true
  78. - name: Set the hostname from mapping file
  79. hostname:
  80. name: "{{ host_name.stdout }}"
  81. when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and (mapping_file | bool == true )
  82. ignore_errors: true
  83. - name: Set the hostname if hostname not present mapping file
  84. hostname:
  85. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  86. when: ('localhost' in hostname_check.stdout) and (file_present.rc != 0) and (mapping_file | bool == true )
  87. ignore_errors: true
  88. - name: Set the system hostname
  89. hostname:
  90. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  91. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false)
  92. ignore_errors: true
  93. - name: Add new hostname to /etc/hosts from mapping file
  94. lineinfile:
  95. dest: /etc/hosts
  96. regexp: '^127\.0\.0\.1[ \t]+localhost'
  97. line: "127.0.0.1 localhost {{ host_name.stdout }}"
  98. state: present
  99. when: ('localhost' in hostname_check.stdout) and ( mapping_file_present != "" ) and ( mapping_file | bool == true )
  100. ignore_errors: true
  101. - name: Add new hostname to /etc/hosts if hostname not present mapping fil
  102. lineinfile:
  103. dest: /etc/hosts
  104. regexp: '^127\.0\.0\.1[ \t]+localhost'
  105. line: "127.0.0.1 localhost compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  106. state: present
  107. when: ('localhost' in hostname_check.stdout) and ( file_present.rc != 0 ) and ( mapping_file | bool == true )
  108. ignore_errors: true
  109. - name: Add new hostname to /etc/hosts
  110. lineinfile:
  111. dest: /etc/hosts
  112. regexp: '^127\.0\.0\.1[ \t]+localhost'
  113. line: "127.0.0.1 localhost compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  114. state: present
  115. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false )
  116. ignore_errors: true
  117. - name: Update inventory
  118. hosts: localhost
  119. connection: local
  120. gather_facts: false
  121. tasks:
  122. - name: Update inventory file
  123. block:
  124. - name: Fetch facts and add new hosts
  125. include_tasks: add_host.yml
  126. with_items: "{{ groups['reachable'] }}"
  127. when: "'reachable' in groups"
  128. - name: Show unreachable hosts
  129. debug:
  130. msg: "{{ host_unreachable_msg }} + {{ groups['ungrouped'] }}"
  131. when: "'ungrouped' in groups"