Query Interface

Version 9 (Ohad Levy, 06/10/2010 03:05 am)

1 1
h1. Query Interface
2 1
3 6 Ohad Levy
*Note*: you need to use Foreman 0.1-3 or latest git version for this feature.
4 6 Ohad Levy
5 7 Ohad Levy
It's possible to use Foreman database to feed other applications - e.g. ssh or use it for cmdb like operations
6 2 Ohad Levy
7 1
h2. Provide host list based on facts and classes
8 1
9 1
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.
10 1
11 1
an additional ruby library (source:extras/query/foreman.rb)  is available to write your own scripts.
12 5 Ohad Levy
13 1
in any case, make sure that you edit foreman.rb to point to your foreman installations (defaults to http://foreman:3000).
14 5 Ohad Levy
15 6 Ohad Levy
h3. Example Script
16 4 Ohad Levy
17 6 Ohad Levy
<pre>
18 4 Ohad Levy
Usage: ./ssh_using_foreman
19 4 Ohad Levy
execute commands on multiple hosts
20 4 Ohad Levy
  Query Foreman to provide host list based on collections of facts and classes
21 4 Ohad Levy
22 4 Ohad Levy
    -c, --command CMD                command to execute
23 4 Ohad Levy
    -u, --user USER                  User to use - defaults to root
24 4 Ohad Levy
    -f, --facts fact=x,fact=y..      one or more facts to filter the host list
25 4 Ohad Levy
    -p, --puppetclass CLASSA,CLASSB  one or more classes to filter the host list
26 8 Ohad Levy
    -s, --state STATE                Filter base of host state - active,out_of_sync all defaults to active hosts
27 4 Ohad Levy
    -h, --help                       Display this screen
28 4 Ohad Levy
</pre>
29 4 Ohad Levy
30 4 Ohad Levy
31 4 Ohad Levy
<pre>
32 4 Ohad Levy
ssh_using_foreman  -c 'uname -i' -f operatingsystem=RedHat,operatingsystemrelease=5
33 4 Ohad Levy
About to execute: uname -i
34 4 Ohad Levy
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
35 4 Ohad Levy
ctrl-c to abort or any key to continue
36 4 Ohad Levy
37 4 Ohad Levy
error connecting to root@subla015.sub.domain: Net::SSH::AuthenticationFailed (root@subla015.sub.domain)
38 4 Ohad Levy
error connecting to root@subla008.sub.domain: Net::SSH::AuthenticationFailed (root@subla008.sub.domain)
39 4 Ohad Levy
[subla077.sub.domain] x86_64
40 4 Ohad Levy
[subla124.sub.domain] x86_64
41 4 Ohad Levy
[subla001.sub.domain] i386
42 4 Ohad Levy
[subla002.sub.domain] i386
43 4 Ohad Levy
[sublc090.sub.domain] x86_64
44 4 Ohad Levy
[subla003.sub.domain] i386
45 4 Ohad Levy
[subla001.sub.domain] i386
46 4 Ohad Levy
[subla002.sub.domain] x86_64
47 4 Ohad Levy
[subla004.sub.domain] i386
48 4 Ohad Levy
[subla001.sub.domain] i386
49 4 Ohad Levy
[sublc084.sub.domain] x86_64
50 4 Ohad Levy
[subla030.sub.domain] i386
51 4 Ohad Levy
[subla003.sub.domain] x86_64
52 4 Ohad Levy
[sublc005.sub.domain] x86_64
53 4 Ohad Levy
[subla001.sub.domain] i386
54 4 Ohad Levy
[sublakiew048.sub.domain] i386
55 4 Ohad Levy
[subla006.sub.domain] i386
56 4 Ohad Levy
[subla114.sub.domain] x86_64
57 4 Ohad Levy
[sublav03.sub.domain] x86_64
58 4 Ohad Levy
[subla005.sub.domain] i386
59 4 Ohad Levy
[subla001.sub.domain] i386
60 4 Ohad Levy
[sublcelba41.sub.domain] x86_64
61 4 Ohad Levy
[sublalic01.sub.domain] x86_64
62 4 Ohad Levy
[subla033.sub.domain] x86_64
63 4 Ohad Levy
[lnzla001.lnz.domain] i386
64 4 Ohad Levy
65 4 Ohad Levy
</pre>
66 4 Ohad Levy
67 4 Ohad Levy
68 1
h2. Fetch the host list from a puppet manifest / template
69 1
70 9 Ohad Levy
see example puppet foreman module - it includes a "custom puppet function":http://github.com/ohadlevy/puppet-foreman/blob/master/foreman/plugins/puppet/parser/functions/foreman.rb