Files
ansible/generate_inventory.sh
2023-08-17 23:08:37 +02:00

19 lines
543 B
Bash
Executable File

#!/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"