Script to format server list into ansible format.

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 

Tar Command Examples

This page provides few examples of tar command which will be useful.
Lets create a folder named justgeek and some files in it.

$ mkdir justgeek
$ cd justgeek/
$ touch 1.txt 2.txt 3.txt 4.txt 5.txt

To create a new tar archive.

$ tar -czvf justgeek.tar.gz justgeek
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

Here is a another command you can use, this can command can be called a shortcut to create a tar file. It will add .tar.gz to the same the folder.

$ tar -czvf justgeek{.tar.gz,}
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

How to extract a tar archive

$ tar -xvf justgeek.tar.gz
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

To view the contents of tar archive without extracting it.

$ tar -tvf justgeek.tar.gz
drwxrwxr-x justgeek/justgeek 0 2019-04-03 13:15 justgeek/
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/1.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/2.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/4.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/3.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/5.txt