Hammer command to list hosts with host collections

Latest response

Hello everyone,

I am looking to see if there is a hammer command to be able to list all the content hosts along with which host collection groups they are in. I am using 6.3.3.

I am reviewing the hammer list and cannot find such a command:
https://access.redhat.com/documentation/en-us/red_hat_satellite/6.3/pdf/hammer_cli_guide/Red_Hat_Satellite-6.3-Hammer_CLI_Guide-en-US.pdf

hammer host list --organization-id x

Does not show the host collection groups, but just shows the Host Group. Not what I am looking for.

My initial way of doing such was to use a search of:

host_collection != "1st Dev Test" and host_collection != "1st Prd Test" and host_collection != "2nd Dev Test" and host_collection != "2nd Prd Test"

I have tried to replace the "and" with "or" and have the same results.

However, when you combine searches like that you do not get results, which is a bug: https://bugzilla.redhat.com/show_bug.cgi?id=1635331 and according to support there is no ETA to have this resolved.

My use for this is that I am patching content hosts based off of the host collection groups they are in. Adding the patching host collection group to the content host is a manual process so I want to make sure there is some sort of auditing.

Is there another way to display the information that I am looking for?
Thanks!

Responses

I came up with this scrip, which is commented for readability:
#!/bin/bash

### UNCOMMENT NEXT 2 LINES FOR DEBUGGING
#set -x
#trap read debug

#check if there is a "-h" or "--help" for help, or no $1
[ "$1" = "--h" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] && echo "This script gets all hosts in a list by host collection:"

#clean working files
/usr/bin/rm hc_*


echo "getting all host collections with hosts and putting them in files"
# get hosts collection ID's
        hammer --csv host-collection list | grep -v "Name,Limit"| cut -d, -f1 | while read hcnum; do
#get host_collection_name
            hcname=`hammer --csv host-collection info --id=$hcnum | tail -1 | cut -d, -f2`
# put hosts from host collections into list
            hammer --csv host-collection hosts --id $hcnum| grep -v ID | cut -d, -f2 | sort >> hc_"$hcname"
        echo "still working"
        done
echo "next step, getting all hosts"
#clean hosts_and_hc file so it is frech
>hosts_and_hc

#get list of all hosts and loop through them
hammer --csv host list| grep -v Name | cut -d, -f2 | while read host; do
    ## echo the hostname to the file
    echo $host >> hosts_and_hc
    ## echo the host collections the host is found in to the file
    grep $host hc* >> hosts_and_hc
done
echo "completed! Check the file hosts_and_hc for a list of all hosts and the host collection the belong to"

Hi, Based on Paul's bash script I have done something similar using Perl:


#!/usr/bin/perl # list all the defined host collections and the hosts assigned to each host collection # list any hosts not assigned to a host collection # # Output is in the form of either: # <host collection name>,<host collection id>,<host name>,<host id> # UNASSIGNED_HOST:<host name> # use strict; my %seen_host; my %seen_host_in_hc; # Get all hosts..... my @all_hosts = `hammer --csv host list`; foreach my $line (@all_hosts) { chomp $line; if ($line =~ /^(\d+?),(.*?),.*/) { my $host_id = $1; my $host_name = $2; $seen_host{$host_id} = $host_name; } } # Get all host collection ids... my @all_hcs = `hammer --csv host-collection list`; foreach my $line (@all_hcs) { chomp $line; if ($line =~ /^(\d+?),(.*?),.*/) { my $hc_id = $1; my $hc_name = $2; # Get hosts assigned to each host collection... my @hosts_in_hcs = `hammer --csv host-collection hosts --id $hc_id`; foreach my $entry (@hosts_in_hcs) { if ($entry =~ /(\d+?),(.*)/) { my $hc_host_id = $1; my $hc_host_name = $2; print "$hc_name,$hc_id,$hc_host_name,$hc_host_id\n"; # add the host_id to a hash if it's associated with a host collection... $seen_host_in_hc{$hc_host_id} = 1; } } } } # print out hosts not associated with a host collection..... my @unassigned_hosts = (); foreach (keys %seen_host) { push(@unassigned_hosts, $seen_host{$_}) unless exists $seen_host_in_hc{$_}; } foreach my $unassigned_host (@unassigned_hosts) { print "UNASSIGNED_HOST:$unassigned_host\n"; } exit(0);

Graham if add headers also .

HI there,

hammer shell

Welcome to the hammer interactive shell. Type 'help' for usage information. hammer> host list --search "lifecycle_environment = Pilot" it will give the list