How can I add a class to a host using the API?

Hi,

I’m pretty new to Foreman and the API, but I am hoping someone can help me
with what might be a pretty straight forward for someone more advanced than
me with this sort of thing. In short I am trying to use the Foreman API to
add a class to a host. The host is first added to a Host Group (which
contains a short list of classes) with the following command:-

curl --user <user/passwd> -X PUT -H "Content-Type:application/json" -H
"Accept:application/json" -d ' {host : {"hostgroup_id":<id#>, "comment":New
Linux Build}}' http://puppetserver.domainname.com:3000/hosts/<hostname>

I now want to add an additional class to this hosts list of classes but
cannot figure it out. There is a puppet_class_ids option listed in the
documentation but I cannot find any example on how to use it and it seems
to suggest it may well overwrite any previously assigned classes to that
host.

Any help on this would be greatly appreciated

Thanks
Conor

Hey Martin,

Thanks for your suggestion, I'll give that a go and let you know how it
goes. Thanks again!

Hi Connor,

Best option is IMHO to use POST /api/hosts/:host_id/puppetclass_ids.
curl --user admin -X POST -H "Content-Type:application/json" -H
"Accept:application/json" -d ' {"puppetclass_id": 8}'
http://fqdn:3000/api/v2/hosts/1/puppetclass_ids

You can also get list of puppetclasses assigned to the host with GET
/api/hosts/:host_id/puppetclasses. Updating host's puppet_class_ids with
host update API call will most likely replace the current classes so
you'd have to get the list first, update it and pass it back in.

If you want to experiment with API it may be useful to try Hammer CLI
with -d option (debug mode) and observe the API calls and parameters it
sends.

Cheers,
Martin

··· On 01/26/2015 11:52 PM, drconorcasey@gmail.com wrote: > > Hi, > > I’m pretty new to Foreman and the API, but I am hoping someone can > help me with what might be a pretty straight forward for someone more > advanced than me with this sort of thing. In short I am trying to use > the Foreman API to add a class to a host. The host is first added to a > Host Group (which contains a short list of classes) with the following > command:- > > curl --user -X PUT -H "Content-Type:application/json" -H > "Accept:application/json" -d ' {host : {"hostgroup_id":, > "comment":New Linux Build}}' > http://puppetserver.domainname.com:3000/hosts/ > > > I now want to add an additional class to this hosts list of classes > but cannot figure it out. There is a puppet_class_ids option listed in > the documentation but I cannot find any example on how to use it and > it seems to suggest it may well overwrite any previously assigned > classes to that host. >