List hosts or content-hosts per host-collection

Latest response

My Satellite clients are grouped into several categories of host-collections. Now I want to select them by host-collection. Best would be a hammer list command with an option "host-collection MYCOLLECTION" but I would already be happy if I could get a list of all hosts with their host-collection.

Apologies if asked at the wrong place and/or stupid question.

Responses

Hello

Try:

hammer host-collection list --help

Maybe bookmark the Getting Help section of the Hammer CLI guide.

Thanks, Stephen, I did try "hammer host-collection list" with all kinds of options in various combinations. It does list host-collections but I found no way to make it also show the hosts in the listed host-collections.

Hello,

OK, sorry for my misunderstanding.

I just created a host collection Test_HC and added one machine rhel7-3-server-test.

hammer host list --search Test_HC
---|-------------------------|---------------|------------|-|---------
ID | NAME                    | OPERATING SYS | HOST GROUP | | CONTENT 
---|-------------------------|---------------|------------|-|---------
8  | rhel7-3-server-test.sat | RedHat 7.3    | TestHG-1   | | Default 
---|-------------------------|---------------|------------|-|---------

I trimmed the output a bit. Hope it displays OK. (2nd try)

Yep that is what I needed. It is unexpected that one can --search for fields that are not in the default out. Thanks, and no need for apologies! I know well enough how easy it is to (partly or completely) misunderstand a question.

Search is one of the most powerful functions. Basically anything that can be searched for in the UI is capable of being passed to hammer.

The search that Stephen provided above is a free-text search and will match (in his example) on any property of the host that contains Test_HC.

For a proper host-collection search (that truly tests membership), you'd want to use

hammer host list --search "host_collection = Test_HC"

NOTE: You can use AND, OR, and NOT to better search. Example (to find systems that are in the Test_HC host collection AND are virtual):

 hammer host list --search "host_collection = Test_HC and facts.is_virtual = true"

For getting a list of hosts for each host-collection, I guess you first have get a list of all host-collections, then use that to get the hosts for each.

I use this script:

#!/bin/bash
# 2017-04-27 tetra
# List all host collections, and the hosts in them

ORG="<add your org here>"

trap "break; exit" INT

readarray -t LINES < <(hammer --output csv host-collection list --organization ${ORG})

for (( i=1; i<${#LINES[@]}; i++))
do
    IFS="," VALS=(${LINES[$i]})
    echo
    echo "${VALS[1]} (${VALS[3]})"
    hammer host list --search "host_collection = ${VALS[1]}" --organization ${ORG}
done

Here's one that I use to just gather the host names of the systems in a given host collection

hammer --output csv host-collection hosts --name "$1" --organization-id 1 | sed '1d' | cut -d ',' -f2 
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.