Foreman ruby api adding node to hostgroup or puppetclasses to node

I wanted to apply puppet classes to a node using a script, I started looking at the foreman REST API but stumbled upon the foreman_api ruby. I specified hostgroups in foreman and added puppet classes to the hostgroups. The idea is that I want to be able to change the hostgroups using a script.

Running things through irb this is what I came up with for changing the hostgroups.


require 'rubygems'
require 'foreman_api'


hosts =
:base_url => '',
:username => 'apiuser',
:password => 'PacktPub'

hosts.update({"id" => hostname, "host" => {"hostgroup_id"=>2}})

Hammer-cli looks a little more promising for this sort of programmed changing of foreman parameters. With hammer I was able to add arbitrary classes to a node.

$ hammer host update --name --puppetclass-ids 14,35
Host updated

With either method I need to know the id's of the hostgroups or the puppetclasses.
I can get those with either though, but I still need to work them out first.

$ hammer puppet_class list
14 | example_one
35 | example_two

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About the Author...

Slides from LISA 2019 Linux systems troubleshooting #LISA2019 Tue Oct 29 05:59:30 +0000 2019 configuring grub2 with EFI Fri Sep 13 05:20:01 +0000 2019

I published a Thing on @thingiverse! #thingalert Tue Jul 23 19:27:57 +0000 2019

Nokogiri install on MacOSX Fri Jul 12 15:06:49 +0000 2019

HTML email with plain mailer plugin on Jenkins Thu Jul 11 21:07:25 +0000 2019