Foreman API : hosts classes?

Hi,

I'm wondering if there's a way to list all hosts and their associated classes, without having to query the api for each host ?
Currently, with /api/hosts, I only this kind of thing :

{"host":{"name":"myhostname","id":139,"hostgroup_id":5,"operatingsystem_id":1}}

But even if the hostgroup has classes associated with it which I can parse, this host in particular has other classes which apparently aren't listed here…
I've found the classes in "/api/hosts/myhostname/puppetclasses?format=json", but I would like to avoid having to do an api call for each host… is that possible ?

Thanks

>
> Hi,
>
> I’m wondering if there’s a way to list all hosts and their associated
> classes, without having to query the api for each host ?
>
> Currently, with /api/hosts, I only this kind of thing :
>
> {"host":{"name":"myhostname","id":139,"hostgroup_id":5,"operatingsystem_id":1}}
>
> But even if the hostgroup has classes associated with it which I can
> parse, this host in particular has other classes which apparently
> aren’t listed here…
>
> I’ve found the classes in
> “/api/hosts/myhostname/puppetclasses?format=json“, but I would like to
> avoid having to do an api call for each host… is that possible ?
>
> Thanks
>
> –
> You received this message because you are subscribed to the Google
> Groups "Foreman users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to foreman-users+unsubscribe@googlegroups.com.
> To post to this group, send email to foreman-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/foreman-users.
> For more options, visit https://groups.google.com/groups/opt_out.
There may be better ways, but what I would try is to use scoped search.

I didn't found any query to get host and classes joined together, so you
have to do it in more requests.
Here are some clues you could use to make your search more optimal.
I also added equivalent for hammer (CLI) as it may be easier to use in
some cases (and it has export to csv).

You can list hosts by class:
> hammer host list --search 'class=ntp'
API: GET "/api/hosts?search=class%3Dntp"

You can list classes by host:
> hammer puppet_class list --search
'host=centos.mbacovsk.com|host=foremandevel.mbacovsk.com'
API: GET
"/api/puppetclasses?search=host%3Dcentos.mbacovsk.com%7Chost%3Dforemandevel.mbacovsk.com"

You can list classes being used by any host:
> hammer puppet_class list --search 'set? host'
API: GET "/api/puppetclasses?search=set%3F+host"

HTH,

Martin

··· On 11/21/2013 10:10 AM, SCHAER Frederic wrote:

Hi,

I'm wondering if there's a way to list all hosts and their associated classes, without having to query the api for each host ?
Currently, with /api/hosts, I only this kind of thing :

{"host":{"name":"myhostname","id":139,"hostgroup_id":5,"operatingsystem_id":1}}

But even if the hostgroup has classes associated with it which I can parse, this host in particular has other classes which apparently aren't listed here…
I've found the classes in "/api/hosts/myhostname/puppetclasses?format=json", but I would like to avoid having to do an api call for each host… is that possible ?

Thanks

··· De : foreman-users@googlegroups.com [mailto:foreman-users@googlegroups.com] De la part de Martin Bacovský Envoyé : jeudi 21 novembre 2013 21:37 À : foreman-users@googlegroups.com Objet : Re: [foreman-users] foreman API : hosts classes ?

On 11/21/2013 10:10 AM, SCHAER Frederic wrote:

You received this message because you are subscribed to the Google Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscribe@googlegroups.commailto:foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.commailto:foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.
There may be better ways, but what I would try is to use scoped search.

I didn’t found any query to get host and classes joined together, so you have to do it in more requests.
Here are some clues you could use to make your search more optimal.
I also added equivalent for hammer (CLI) as it may be easier to use in some cases (and it has export to csv).

You can list hosts by class:

hammer host list --search 'class=ntp’
API: GET “/api/hosts?search=class%3Dntp”

You can list classes by host:

hammer puppet_class list --search 'host=centos.mbacovsk.com|host=foremandevel.mbacovsk.com
API: GET “/api/puppetclasses?search=host%3Dcentos.mbacovsk.com%7Chost%3Dforemandevel.mbacovsk.com

You can list classes being used by any host:

hammer puppet_class list --search 'set? host’
API: GET “/api/puppetclasses?search=set%3F+host”

HTH,

Martin

Hi,

Thanks a lot :]
I’ve discovered hammer this week , I haven’t had time to try it but I will .

I wasn’t aware the api could be searched this way, thanks a lot I’ll try that to see what I can get with this !

Regards