diff --git a/site/profiles/templates/helpers/node_lookup.erb b/site/profiles/templates/helpers/node_lookup.erb index 7157f76..deeb39e 100644 --- a/site/profiles/templates/helpers/node_lookup.erb +++ b/site/profiles/templates/helpers/node_lookup.erb @@ -28,10 +28,23 @@ def query_puppetdb(query): response = requests.get(url, params={'query': query}) process_response(response) -def process_response(response): +def process_response(response, count_only=False): if response.status_code == 200: - for fact in response.json(): - print(f"{fact['certname']} {fact['value']}") + try: + response_data = response.json() + except ValueError: + print("Error decoding JSON response") + return + + if count_only: + fact_counter = Counter(fact['value'] for fact in response_data) + for fact_value, count in fact_counter.items(): + print(f"{fact_value}: {count}") + else: + facts = [f"{fact['certname']} {fact['value']}" for fact in response_data] + facts.sort() + for fact in facts: + print(fact) else: print(f"Error querying PuppetDB: HTTP {response.status_code}") print("Response content:", response.text) @@ -46,6 +59,7 @@ def main(): parser.add_argument("-R", "--role", action="store_true", help="Show the role for matched hosts") parser.add_argument("-F", "--fact", help="Specify a fact name") parser.add_argument("-m", "--match", help="Simple pattern match for the value") + parser.add_argument("-C", "--count", action="store_true", help="Show count of rows with the same fact") args = parser.parse_args()