Skip to content

Instantly share code, notes, and snippets.

@aabouzaid
Last active August 29, 2015 14:13
Show Gist options
  • Save aabouzaid/066a3e5e026c599ec196 to your computer and use it in GitHub Desktop.
Save aabouzaid/066a3e5e026c599ec196 to your computer and use it in GitHub Desktop.
#!/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