Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Last active November 5, 2018 19:23
Show Gist options
  • Save dishbreak/ce868531c4e85bb3901c8affdb5a59d7 to your computer and use it in GitHub Desktop.
Save dishbreak/ce868531c4e85bb3901c8affdb5a59d7 to your computer and use it in GitHub Desktop.
For each role on a chef server, display the number of servers with that role.
#!/bin/bash
set -e
if ! which knife; then
( >&2 echo "Couldn't find knife on your path!" )
exit 1
fi
# Redirecting stderr to /dev/null will make output clean.
ROLES=$(knife role list 2>/dev/null)
# Bash delimits on whitespace, so you can use this to iterate over every role on the chef server.
for ROLE in $ROLES; do
# the -e flag returns the number of matches.
SERVER_COUNT=$(knife search "role:$ROLE" 2>/dev/null | grep -e "Node Name" )
echo "$SERVER_COUNT $ROLE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment