Script to format list of servers into ansible format.
Example:
if you have a text file which has list of servers as below.
app.server.com
app2.server.com
web1.server.com
web2.server.com
[app]
app.server.com
app2.server.com
[web]
web1.server.com
web2.server.com
How to use it ?
create a file generateInventory.sh with the contents below and it the same directory create a file named raw_inventory.txt with list of the servers and run the script using ./generateInventory.sh and you will get ansible format Inventory file will generated.
# Script to format list of servers into ansible format.
file=raw_inventory.txt
last=''
while read nameX
do label="${nameX%%[.0-9]*}"
if [[ "$label" != "$last" ]]
then echo "[$label]" # or printf "\n$label\n" for a separator line
last="$label"
fi
echo "$nameX"
done < $file > Inventory