#!/bin/bash # Scan the network to discover hosts nmap -sn 192.168.1.0/24 -oG - | awk '/Up$/{print $2}' > hosts.txt # Create the Ansible inventory file in YAML format echo "all:" > inventory.yml while read -r ip_address; do dns_name=$(dig -x "$ip_address" +short | sed -e 's/\.$//') if [ -n "$dns_name" ]; then echo " hosts:" >> inventory.yml echo " $dns_name:" >> inventory.yml echo " ansible_host: $ip_address" >> inventory.yml fi done < hosts.txt echo "Inventory file created: inventory.yml"