Last active
August 29, 2015 14:13
-
-
Save aabouzaid/066a3e5e026c599ec196 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#5 minutes Ansible module to list groups in inventory :D | |
#You can see outupt of script like "JSON Pretty Print" outside Ansible by using: | |
#./listgroups | python -m json.tool | |
inventory_file=$(awk '/^hostfile/{print $3}' /etc/ansible/ansible.cfg) | |
groups_array=($(awk 'BEGIN {ORS=" "}; /^\[/{gsub("[\[\]]",""); print}' $inventory_file)) | |
init_array_for_json () { | |
for group in ${groups_array[@]}; do | |
printf "\"$group\","; | |
done | |
} | |
printf "{\"Inventory Groups\": [$(init_array_for_json)]}" | sed 's/,]/]/g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment