create_inventory.yml 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  46. tasks:
  47. - name: Setup
  48. setup:
  49. filter: ansible_*
  50. - name: Set the system hostname
  51. hostname:
  52. name: "compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}"
  53. register: result_name
  54. - name: Add new hostname to /etc/hosts
  55. lineinfile:
  56. dest: /etc/hosts
  57. regexp: '^127\.0\.0\.1[ \t]+localhost'
  58. line: "127.0.0.1 localhost 'compute{{ inventory_hostname.split('.')[-2] + '-' + inventory_hostname.split('.')[-1] }}'"
  59. state: present
  60. - name: Update inventory
  61. hosts: localhost
  62. connection: local
  63. gather_facts: false
  64. tasks:
  65. - name: Update inventory file
  66. block:
  67. - name: Fetch facts and add new hosts
  68. include_tasks: add_host.yml
  69. with_items: "{{ groups['reachable'] }}"
  70. when: "'reachable' in groups"
  71. - name: Show unreachable hosts
  72. debug:
  73. msg: "{{ host_unreachable_msg }} + {{ groups['ungrouped'] }}"
  74. when: "'ungrouped' in groups"