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.

#!/usr/bin/ruby

require 'rubygems'
require 'foreman_api'

hostname='node1.example.com'

hosts = ForemanApi::Resources::Host.new(
  {
    :base_url => 'http://foreman.example.com',
    :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 node1.example.com --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
-------------------------------
ID | NAME                      
-------------------------------
14 | example_one
35 | example_two                      
-------------------------------
$

Wordpress category: 

Comments

Greet!

Add new comment

Markdown

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.