Skip to content

Hammer time! Using the Foreman CLI to provision hosts

Hammer

Meeting Hammer

Wandering around in /usr/bin I found Hammer, the tool of the Foreman of which the site of Documentation had spoken. However, each time I tried to communicate with it, it kept asking me for the same information over and over again. Username, password, server. I then discovered that when I put this information into /home<user>/.hammer/cli_config.yml  it stopped asking me for the information. I am however a bit worried leaving such information in a file but since it’s safeley stored in my /home it shouldn’t be that big of a deal and when I leave I could remove the password if I wished so.

Here is the information I provided in the file:

:foreman:
 :host: 'https://satellite.myredfedora.com/'
 :username: 'username'
 :password: 'Password'

 

Using Hammer

When asking Hammer to do something now, it’s eager to help me out and gives me advise on how to phrase my questions. Sometimes he takes quite awhile but most of the time he comes back with the information I was looking for. For instance if I wanted to see which hosts are registered to my Satellite 6, I could run the following:

hammer host list --per-page 3
----|-------------------------------------------|------------------|--------------------|----------------|------------------
ID  | NAME                                      | OPERATING SYSTEM | HOST GROUP         | IP             | MAC 
----|-------------------------------------------|------------------|--------------------|----------------|------------------
201 | machine1.myredfedora.com                  | RHEL 6.8         | HG_ACCP_HW_6       | 10.1.1.1       | xx:xx:xx:xx:xx:xx
205 | machine2.myredfedora.com                  | RHEL 6.8         | HG_ACCP_VM_6       | 10.2.2.2       | xx:xx:xx:xx:xx:xx
177 | machine3.myredfedora.com                  | RHEL 7.3         | HG_PROD_VM_7       | 10.3.3.3       | xx:xx:xx:xx:xx:xx

 

Can’t touch this

Hammer has a few limitations and one of them is getting detailed information from a virtualization platform, if for instance I wanted to know the free space of a datastore, hammer won’t be able to help me out. Luckily Satellite 6 comes with an awesome API, in the script below I’ve used this API to gather information on the available space in the datastores. I will talk about the API and how to use it in a future post as this is to comprehensive to fit into this post.

The power of the hammer

Hammer has quite a range of useful command’s which can be used either to get or to set configuration items. One of the more powerful commands is the  host  command, which let’s you manage and even provision hosts. This feature really shines when you combine it with scripting. You can very easily create a for-loop which iterates trough a csv file to create a large number of VM’s fairly quickly. If you have PXE setup and have a virtualization platform defined as a compute resource, you can provision hosts really fast.

Below I have written a simple script to get started with scripted provisioning. My environment consists of a VMware setup which I’ve setup as a Compute Resource, the exact permissions are detailed on the Red hat website . Further I’ve setup PXE to make sure hosts can boot from the network and I don’ t have to bother with creating ISO’s or images for VMware. Last but not least I’ve setup some kickstart and post-install scripts to make sure my new machines get the settings and tools needed to operate properly in my network. For the moment I’m skipping puppet and I’m waiting for 6.3 to get my hands on Ansible

 

#!/bin/bash

# Make sure you have credentials set in /home/<user>/.hammer/cli_config.yml
# Content should look like this:
#
# :foreman:
# :host: 'https://satellite.myredfedora.com/'
# :username: 'username'
# :password: 'passwor'


# Setting variables

vmname="$1"
vmip="$2"
datastore="$3"

# Functions

function datast () {
# For this function to function, you will need to create a 'viewer' account on your Satellite 6 install.
# Also to display the json information in a readable format you'll need python, the json.tool module should come with it.
 USER="username"
 PASS="password"
 FOREMAN_URL="https://satellite.myredfedora.com/api"

 stores=$(curl -s -H "Accept:application/json" \
 -k -u $USER:$PASS \
 $FOREMAN_URL/compute_resources/1/available_storage_domains | python -m json.tool \
 )

 echo "${stores}"
}

### Script start ###
#
# If not all arguments are set, the script will get and output the freespace in the datastores
# 

if [ "${datastore}" == "" ]; then
 echo ''
 echo ''
 echo 'Usage: ./$0 <vmname> <mgt ip address> <datastore>'
 echo ''
 echo '' 
 echo '--- Checking datastore availability ---'
 echo ''
 datast
 echo ''
 exit
fi


# Replace with line below to enable troubleshooting
# hammer -d host create \
# Edit all settings below, read all lines carefully.
# Everything prepended with an ! should be changed to 
# either a variable you can create or something appropriate for your environment.
# Also please mind that I used some defaults you might want to change.

hammer host create \
--name "${vmname}" \
--environment !System_Environment \
--organization !YourOrg \
--location-id !NumeralLocationId \
--lifecycle-environment !Your_Lifecycle_Environment \
--hostgroup !Host_Group \
--operatingsystem "!RHEL X.X" \
--architecture "!x86_64" \
--domain-id !Numeral_domain_Id \
--subnet !Subnet_Name \
--ip "${vmip}" \
--compute-resource-id !Numeral_compute_Resource_Id \
--provision-method !build or image \
--content-view !Your_Content_View \
--compute-attributes "cpus=1,corespersocket=1,cluster=!YourCluster,path=!/Datacenters/YourOrg/Linux/SomeSubFolder,guest_id=!rhel7_64Guest,memory_mb=1024,start=1,scsi_controller_type=ParaVirtualSCSIController" \
--interface "primary=true,managed=true,provision=true,execution=true,compute_type=VirtualVmxnet3,type=interface,compute_network=!YourNetwork,_destroy=0,virtual=0" \
--build !true \
--volume "datastore="${datastore}",thin=true,eager_zero=false,size_gb=20G" \
--compute-profile-id !1 \
--ask-root-password no \

 

 

It’s not the end

Since I’ve only just begun tinkering with hammer the past week I’ll be revisiting this subject in the near future.  I can only imagine what more cool stuff you can do with this tool and I’m sure I’ll figure out more about the API and how to integrate this in a fancy way with hammer some more.