Import hosts into foreman

Dear Sirs/Madams,

I have been searching for an answer to this for some time, but unfortunately i cannot find an explanation on how i can achieve this?

Is there a way to import a list of hosts from a text file?

I.e say i have a file with 200 hosts containing :
hostname , mac address, ip address, host group

I would love to avoid having to manually enter these details into the foreman web interface.

Many thanks in advanced!

Br
PG

The easiest path would be writing a script that reads the text file and calls similar command for each record

hammer host create --build false --name $hostname --managed false --ip $ip --mac $mac --hostgroup $hostgroup

Note that the hostgroup needs to be created separately.

Hope this helps

1 Like

Thank you very much Marek!

Exactly what i was looking for!

Works perfectly!

Best Regards
PG

Just in case someone else also need to import a list of hosts to Foreman, here’s a simple script that use a csv file as an argument.

Tested with Foreman 2.4

tfm_csv_import_hosts.sh

#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin

INPUT=$1
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read hostname ip mac hostgroup location organization interface environment domain subnet; do
        hammer host create \
                --build false \
                --name $hostname \
                --managed true \
                --ip $ip \
                --mac $mac \
                --hostgroup $hostgroup \
                --location \"$location\" \
                --organization \"$organization\" \
                --interface \"identifier=$interface\" \
                --puppet-environment \"$environment\" \
                --domain \"$domain\" \
                --subnet \"$subnet\"
done < $INPUT
IFS=$OLDIFS

Usage:
./tfm_csv_import_hosts.sh hosts-list.csv

CSV file columns should be as follow:

hostname ip mac hostgroup location organization interface name environment domain name subnet name

Feel free to modify the script for your needs.

Hope it helps.

1 Like

During the copy past, backslash were added to some variables. Tried to edit my post, but I couldn’t as it was too late.

So here’s the fixed version:

#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin

INPUT=$1
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read hostname ip mac hostgroup location organization interface environment domain subnet; do
 hammer host create \
         --build false \
         --name "$hostname" \
         --managed true \
         --ip "$ip" \
         --mac "$mac" \
         --hostgroup "$hostgroup" \
         --location "$location" \
         --organization "$organization" \
         --interface "identifier=$interface" \
         --puppet-environment "$environment" \
         --domain "$domain" \
         --subnet "$subnet"
done < $INPUT
IFS=$OLDIFS

Also here’s an example of a CSV content:

hostname ip mac hostgroup location organization interface environment domain subnet
my-compute-01 10.0.0.23 00:ff:ae:bb:cc:dd compute Default Location Default Organization eno1 production mydomain.com OS Network

NOTE: Just do not include columns header in your csv file