create_inventory.yml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: Group reachable hosts
  28. group_by:
  29. key: "reachable"
  30. when: "'100% packet loss' not in ping_result.stdout"
  31. - name: Get provision password
  32. hosts: localhost
  33. connection: local
  34. gather_facts: false
  35. tasks:
  36. - name: Include vars file of inventory role
  37. include_vars: ../vars/main.yml
  38. - name: Set hostname on reachable nodes and gather facts
  39. hosts: reachable
  40. gather_facts: False
  41. remote_user: "{{ cobbler_username }}"
  42. vars:
  43. ansible_password: "{{ cobbler_password }}"
  44. ansible_become_pass: "{{ cobbler_password }}"
  45. tasks:
  46. - name: Setup
  47. setup:
  48. filter: ansible_*
  49. - name: Set the system hostname
  50. hostname:
  51. name: "compute{{ inventory_hostname.split('.')[-2] + '.' + inventory_hostname.split('.')[-1] }}"
  52. register: result_name
  53. - name: Add new hostname to /etc/hosts
  54. lineinfile:
  55. dest: /etc/hosts
  56. regexp: '^127\.0\.0\.1[ \t]+localhost'
  57. line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-1] }}'"
  58. state: present
  59. - name: Ensure networking connection
  60. command: nmcli networking off
  61. changed_when: false
  62. - name: Ensure networking connection
  63. command: nmcli networking on
  64. changed_when: false
  65. - name: Ensure networking connection
  66. command: nmcli networking on
  67. changed_when: false
  68. - name: Update inventory
  69. hosts: localhost
  70. connection: local
  71. gather_facts: false
  72. tasks:
  73. - name: Update inventory file
  74. block:
  75. - name: Fetch facts and add new hosts
  76. include_tasks: add_host.yml
  77. with_items: "{{ groups['reachable'] }}"
  78. when: "'reachable' in groups"
  79. - name: Show unreachable hosts
  80. debug:
  81. msg: "{{ host_unreachable_msg }} + {{ groups['ungrouped'] }}"
  82. when: "'ungrouped' in groups"