Last active
February 8, 2019 16:08
-
-
Save afiune/252c320db2f636bdbc0c3006bdfdb81c to your computer and use it in GitHub Desktop.
Automate 2 Tricks
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 | |
# | |
# Author:: Salim Afiune <[email protected]> | |
# Purpose:: List all nodes from an Automate 2 server that their last chef-client run updated one or more resources | |
# | |
# Requirements: | |
# * curl - https://curl.haxx.se/download.html | |
# * jq - https://stedolan.github.io/jq/ | |
# * API token - https://automate.chef.io/docs/api-tokens/#creating-an-admin-api-token | |
# | |
# Usage:: Update the script with your automate fqdn and a valid admin token, then execute the script. | |
# Pass the parameter 'all' to list all nodes regardless if they have updated resources or not. | |
# | |
# $ ./list_nodes_with_updated_resources.sh all | |
# Node ID | Updated Resources | Node Name | |
# --------------------------------------------------------------- | |
# d92fbcb5-9ccf-3fed-997f-1f77c2e988b3 2 chef-load-1 | |
# 8e6c9a32-36b4-3ca4-840d-1a21957f679c 6 chef-load-2 | |
# 9112748a-c778-3ace-8872-2dcf3c3830df 0 chef-load-4 | |
# ee723f2a-c4b4-3193-887c-3e88cff3f9f5 21 chef-load-999 | |
# | |
set -e | |
automate_fqdn='a2-dev.test' | |
admin_token='muCgwzzqD75p156dFbB5kpxtvxI=' | |
nodes_file='/tmp/nodes.raw' | |
curl "https://${automate_fqdn}/api/v0/cfgmgmt/nodes?pagination.page=1&pagination.size=100&sorting.field=name" -H "api-token: ${admin_token}" --insecure > $nodes_file 2>/dev/null | |
echo " Node ID | Updated Resources | Node Name" | |
echo "----------------------------------------------------------------" | |
n=0 | |
while true; do | |
node_name=$(jq '.['$n'] | .name' $nodes_file | tr -d '"') | |
node_id=$(jq '.['$n'] | .id' $nodes_file | tr -d '"') | |
run_id=$(jq '.['$n'] | .latest_run_id' $nodes_file | tr -d '"') | |
# Break when there are no more nodes to read | |
if [[ "$node_name" == "null" ]]; then | |
break; | |
fi | |
updated_resource_count=$(curl "https://${automate_fqdn}/api/v0/cfgmgmt/nodes/${node_id}/runs/${run_id}" -H "api-token: ${admin_token}" --insecure 2> /dev/null | jq .updated_resource_count) | |
# Print whe the parameter 'all' was provided or the node has updated resources | |
if [[ "$1" == "all" ]] || [[ $updated_resource_count -ne 0 ]]; then | |
echo "$node_id $updated_resource_count $node_name" | |
fi | |
n=$(( n + 1 )) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment