inventory_creation.yml 1.4 KB

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