create_inventory.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. register: result_host_name
  82. when: ('localhost' in hostname_check.stdout) and (mapping_file_present != "" ) and (mapping_file | bool == true )
  83. ignore_errors: true
  84. - name: Set the system hostname
  85. hostname:
  86. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  87. register: result_name
  88. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false)
  89. ignore_errors: true
  90. - name: Add new hostname to /etc/hosts from mapping file
  91. lineinfile:
  92. dest: /etc/hosts
  93. regexp: '^127\.0\.0\.1[ \t]+localhost'
  94. line: "127.0.0.1 localhost {{ host_name.stdout }}"
  95. state: present
  96. when: ('localhost' in hostname_check.stdout) and ( mapping_file_present != "" ) and ( mapping_file | bool == true )
  97. ignore_errors: true
  98. - name: Add new hostname to /etc/hosts
  99. lineinfile:
  100. dest: /etc/hosts
  101. regexp: '^127\.0\.0\.1[ \t]+localhost'
  102. line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}'"
  103. state: present
  104. when: ('localhost' in hostname_check.stdout) and (mapping_file | bool == false )
  105. ignore_errors: true
  106. - name: Update inventory
  107. hosts: localhost
  108. connection: local
  109. gather_facts: false
  110. tasks:
  111. - name: Update inventory file
  112. block:
  113. - name: Fetch facts and add new hosts
  114. include_tasks: add_host.yml
  115. with_items: "{{ groups['reachable'] }}"
  116. when: "'reachable' in groups"
  117. - name: Show unreachable hosts
  118. debug:
  119. msg: "{{ host_unreachable_msg }} + {{ groups['ungrouped'] }}"
  120. when: "'ungrouped' in groups"