Struggling with an API call

I am trying to execute an API call to foreman (latest) in order to list out all the hosts within a specific host collection. This seems likely a combination of calling the hosts api and filter/search but I’ve been struggling to craft the api request.

example:

I make call /api/host_collections/20/hosts" and I see

testserver1.example.com
testserver2.example.com
testserver3.example.com

I realize I’ll likely need to format the json to get something clean like that but that’s my end goal. If I can atleast figure out how to even get the list.

Anyone give me a hand?

Hammer can help a lot here. You can use the -d or debug option to see the exact API request hammer is making. (side note: hammer can also format output as json.)

Unfortunately I needed a solution which could be used with just a plain curl as this is for admins on a jump server. I’m unfamiliar with hammer myself but I assume it would be a possibility to install it on this tools server, setup some sort of API key, and make use of it remotely?

In the meantime, I did figure this out. The curl command in total looks like this for anyone else who needs it. This specifies host id 14 and lists out just the hostnames in an easily copied format. It uses JQ to parse it but you could use other tools.

curl -ks --user username:token -H “Content-Type: application/json” “https://exampleserver.com/api/hosts?search=host_collection_id=14” | jq -r ‘.results.name’

1 Like