Query Interface

Note: you need to use Foreman 0.1-3 or latest git version for this feature.
Note: This feature will be DEPRECATED in the future and replace by the Search API.
Note: The below script has been updated to use the newer API and can be found at the 0.5 version or latest develop branch

It's possible to use Foreman database to feed other applications - e.g. ssh or use it for cmdb like operations

Provide host list based on facts and classes

in the source:extras/query directory you could find an example multi ssh script providing example of how to run a command on all of your hosts that answer a certain criteria.

an additional ruby library (source:extras/query/foreman.rb) is available to write your own scripts.

in any case, make sure that you edit foreman.rb to point to your foreman installations (defaults to http://foreman:3000).

Example Script

Usage: ./ssh_using_foreman
execute commands on multiple hosts
  Query Foreman to provide host list based on collections of facts and classes

    -c, --command CMD                command to execute
    -u, --user USER                  User to use - defaults to root
    -f, --facts fact=x,fact=y..      one or more facts to filter the host list
    -p, --puppetclass CLASSA,CLASSB  one or more classes to filter the host list
    -s, --state STATE                Filter base of host state - active,out_of_sync all defaults to active hosts
    -h, --help                       Display this screen
ssh_using_foreman  -c 'uname -i' -f operatingsystem=RedHat,operatingsystemrelease=5
About to execute: uname -i
on the following 36 hosts: sublalic01.sub.domain subla003.sub.domain sublc005.sub.domain subla001.sub.domain sublakiew048.sub.domain subla001.sub.domain subla077.sub.domain subla124.sub.domain subla002.sub.domain subla033.sub.domain lnzla001.lnz.domain sublav04.sub.domain sublav05.sub.domain subla006.sub.domain subla114.sub.domain subla113.sub.domain subla141.sub.domain subla109.sub.domain sublav03.sub.domain subla005.sub.domain subla001.sub.domain sublc084.sub.domain subla011.sub.domain subla002.sub.domain subla030.sub.domain subla001.sub.domain subla001.sub.domain subla002.sub.domain subla004.sub.domain subla003.sub.domain subla004.sub.domain subla015.sub.domain sublcelba40.sub.domain sublcelba41.sub.domain subla008.sub.domain sublc090.sub.domain
ctrl-c to abort or any key to continue

error connecting to root@subla015.sub.domain: Net::SSH::AuthenticationFailed (root@subla015.sub.domain)
error connecting to root@subla008.sub.domain: Net::SSH::AuthenticationFailed (root@subla008.sub.domain)
[subla077.sub.domain] x86_64
[subla124.sub.domain] x86_64
[subla001.sub.domain] i386
[subla002.sub.domain] i386
[sublc090.sub.domain] x86_64
[subla003.sub.domain] i386
[subla001.sub.domain] i386
[subla002.sub.domain] x86_64
[subla004.sub.domain] i386
[subla001.sub.domain] i386
[sublc084.sub.domain] x86_64
[subla030.sub.domain] i386
[subla003.sub.domain] x86_64
[sublc005.sub.domain] x86_64
[subla001.sub.domain] i386
[sublakiew048.sub.domain] i386
[subla006.sub.domain] i386
[subla114.sub.domain] x86_64
[sublav03.sub.domain] x86_64
[subla005.sub.domain] i386
[subla001.sub.domain] i386
[sublcelba41.sub.domain] x86_64
[sublalic01.sub.domain] x86_64
[subla033.sub.domain] x86_64
[lnzla001.lnz.domain] i386

Fetch the host list from a puppet manifest / template

see example puppet foreman module - it includes a custom puppet function