inventory_creation.yml 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. - hosts: localhost
  2. connection: local
  3. gather_facts: true
  4. tasks:
  5. - name: Read dhcp file for rocky
  6. set_fact:
  7. var: "{{ lookup('file', '/var/lib/dhcpd/dhcpd.leases').split()| unique | select| list }}"
  8. when: ansible_facts['distribution'] | lower == "rocky"
  9. - name: Read dhcp file for leap
  10. set_fact:
  11. var: "{{ lookup('file', '/var/lib/dhcp/db/dhcpd.leases').split()| unique | select| list }}"
  12. when: ("leap" in ansible_facts['distribution'] | lower)
  13. - name: Filter the ip
  14. set_fact:
  15. vars_new: "{{ var| ipv4('address')| to_nice_yaml}}"
  16. - name: Create the static ip
  17. shell: awk -F',' 'NR >1{print $3}' omnia/control_plane/roles/provision_cobbler/files/new_host_mapping_file.csv > static_hosts.yml
  18. changed_when: false
  19. failed_when: false
  20. - name: Create the dynamic inventory
  21. shell: |
  22. echo "{{ vars_new }}" > temp.txt
  23. egrep -o '[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' temp.txt >>dynamic_hosts.yml
  24. changed_when: false
  25. failed_when: false
  26. - name: Final inventory
  27. shell: cat dynamic_hosts.yml static_hosts.yml| sort -ur >> omnia/control_plane/roles/collect_node_info/files/provisioned_hosts.yml
  28. changed_when: false
  29. - name: Temp result
  30. shell: cat /root/omnia/control_plane/roles/collect_node_info/files/provisioned_hosts.yml |sort|uniq
  31. register: inventory
  32. - name: Final Inventory
  33. local_action: copy content="{{ inventory.stdout }}" dest=omnia/control_plane/roles/collect_node_info/files/provisioned_hosts.yml
  34. - name: New line at end of file
  35. shell: echo "">> omnia/control_plane/roles/collect_node_info/files/provisioned_hosts.yml
  36. changed_when: false