Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aabouzaid/abf6f95bfb0fa1196dbe to your computer and use it in GitHub Desktop.
Save aabouzaid/abf6f95bfb0fa1196dbe to your computer and use it in GitHub Desktop.
#!/bin/bash
#10 minutes Ansible module to list groups in inventory (AWK version) :D
#You can see outupt of script like "JSON Pretty Print" outside Ansible by using:
#./listgroups | python -m json.tool
awk 'BEGIN {ORS=" ";};
/^\[/{gsub("[\[\]]",""); groups_number++; groups_array[groups_number]=$0};
END {
print "{\"Inventory Groups\": [";
for (group_in_array = 1; group_in_array <= groups_number-1; group_in_array++) printf "\"%s\", ",groups_array[group_in_array];
printf "\"%s\"",groups_array[groups_number];
print "]}"
}' /etc/ansible/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment