Examples

Perl

the simple perl code below creates a single host and prints the response (maybe some values are missing, this depend on your hostgroups (i have default values in my hostgroups), but it should be easy to extend the perl hash) The script has not much error checking yet.

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

use REST::Client;
use JSON;
use MIME::Base64;
use Data::Dumper;

#your username and password
my $username = 'admin';
my $password = '****';

#replace with your foreman server
my $host = 'http://foreman';

#set the headers, without foreman don't know the type of data and can not work with it
my $headers = {Content_Type => 'application/json', Accept => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)};

my $client = REST::Client->new();

$client->setHost($host);

#set the attributes for a new host
my %json_data = (
                host => {
                        hostgroup_id => '1',
                        name => "testhost",
                        ip => "1.1.1.1",
                        mac => "00:00:00:00:01",
                        environment_id => '1',
                }
        );

#translate the perl hash in json notation
my $data = encode_json(\%json_data);

#create the host
$client->POST('/hosts',($data,$headers));

#decode the response in perl a type
my $response = decode_json ($client->responseContent());

#print the response
print Dumper($response);

}

cURL

#! /bin/sh
#

USER="ci" 
PASS="ci" 
FOREMAN_URL="https://foreman.domain.org" 
NAME=$1
HOSTGROUP_ID="1"  # Change this to your favorite hostgroup

curl -s -H "Accept:application/json" \
     -k -u $USER:$PASS \
     -d "host[name]=$NAME" -d "host[hostgroup_id]=$HOSTGROUP_ID" \
     -d "host[powerup]=1"  -d "host[build]=1" \
     $FOREMAN_URL/hosts | prettify_json.rb