I've started leveraging Foreman to do Extlookups within my Puppet modules,
which was somewhat of a life changing discovery. I've been using Foreman
for over a year now and couldn't believe I didn't know about the foreman
parser function. This was my
reference, http://blog.theforeman.org/2012/01/getting-foreman-search-results-into.html.
I've modified the foreman.rb extlookup script to run as a command line tool
to test the output, https://gist.github.com/3409173, when I run this test
foreman("hostgroups", "host = host1.tamu.edu")
Using script…
ruby foreman_extlookup.rb https://mforeman_host.tamu.edu -i hostgroups -s
"host = host1.tamu.edu" -u lookup -p <omit>
warning: peer certificate won't be verified in this SSL session
Results:
— []
the results are an empty array. The same search in the web interface of
Foreman results in the host's hostgroup. URL used in web
interface, https://myforeman_host.tamu.edu/hostgroups?search=host%20=%20host1.tamu.edu
From rails console…
irb(main):003:0> @host = Host.find_by_name("host1.tamu.edu")
irb(main):004:0> @hostgroup = Hostgroup.search_for("host = #{@host.name}")
···
+----+-------+---------------+----------------+---------------+---------------+---------------+-----------+-----------+----------------+---------------+-----------+------------+----------+----------------+-----------+-----------+---------------+ > id | name | created_at | updated_at | environmen... | operatings... | architectu... | medium_id | ptable_id | root_pass | puppet_ca_... | use_image | image_file | ancestry | vm_defaults | subnet_id | domain_id | puppet_pro... | +----+-------+---------------+----------------+---------------+---------------+---------------+-----------+-----------+----------------+---------------+-----------+------------+----------+----------------+-----------+-----------+---------------+ > 24 | mysql | 2012-08-20... | 2012-08-20 ... | 5 | > 1 | | | $1$foreman$... | 3 > > > 11/23 | {"hyperviso... | | 1 > 3 | +----+-------+---------------+----------------+---------------+---------------+---------------+-----------+-----------+----------------+---------------+-----------+------------+----------+----------------+-----------+-----------+---------------+ 1 row in set irb(main):005:0> @hostgroup.inspect => "[#]" irb(main):006:0>There are no errors in the production.log. This is with foreman-1.0.1-1.el6
The script from Foreman and my modification both have worked great when
querying facts and host values, but this is the first time I’ve done a
search on hostgroups.
Thanks
- Trey
production.log - some items omitted as these are somewhat
security sensitive, publicly routable, servers.
Started GET “/hostgroups?search=host%20=%20host1.tamu.edu” for at
Mon Aug 20 18:53:26 -0500 2012
Processing by HostgroupsController#index as JSON
Parameters: {“search”=>“host = host1.tamu.edu”}
User Load (0.4ms) SELECT users
.* FROM users
WHERE users
.login
=
‘lookup’ LIMIT 1
AuthSource Load (0.1ms) SELECT auth_sources
.* FROM auth_sources
WHERE auth_sources
.id
= 1 LIMIT 1
User Load (0.2ms) SELECT users
.* FROM users
WHERE (login=‘lookup’)
LIMIT 1
Authenticated user External Lookup against INTERNAL authentication source
User Load (0.1ms) SELECT users
.* FROM users
WHERE users
.login
=
‘admin’ LIMIT 1
Setting current user thread-local variable to admin
SQL (0.0ms) BEGIN
AREL (0.2ms) UPDATE users
SET last_login_on
= ‘2012-08-20 23:53:26’,
updated_at
= ‘2012-08-20 23:53:26’ WHERE users
.id
= 3
SQL (139.8ms) COMMIT
Role Load (0.1ms) SELECT roles
.* FROM roles
WHERE roles
.name
=
‘Anonymous’ LIMIT 1
SQL (0.1ms) SELECT 1 FROM roles
INNER JOIN user_roles
ON roles
.id
= user_roles
.role_id WHERE roles
.id
= 8 AND ((user_roles
.user_id =
3)) LIMIT 1
Setting current user thread-local variable to lookup
Setting current user thread-local variable to admin
Authorized user lookup(External Lookup)
Setting current user thread-local variable to lookup
Role Load (0.1ms) SELECT roles
.* FROM roles
INNER JOIN user_roles
ON roles
.id = user_roles
.role_id WHERE ((user_roles
.user_id = 3))
Hostgroup Load (0.1ms) SELECT * FROM hostgroups
INNER JOIN
user_hostgroups
ON hostgroups
.id = user_hostgroups
.hostgroup_id WHERE
(user_hostgroups
.user_id = 3 )
Hostgroup Load (0.5ms) SELECT hostgroups
.id
AS t0_r0,
hostgroups
.name
AS t0_r1, hostgroups
.created_at
AS t0_r2,
hostgroups
.updated_at
AS t0_r3, hostgroups
.environment_id
AS t0_r4,
hostgroups
.operatingsystem_id
AS t0_r5, hostgroups
.architecture_id
AS t0_r6, hostgroups
.medium_id
AS t0_r7, hostgroups
.ptable_id
AS
t0_r8, hostgroups
.root_pass
AS t0_r9, hostgroups
.puppet_ca_proxy_id
AS t0_r10, hostgroups
.use_image
AS t0_r11, hostgroups
.image_file
AS
t0_r12, hostgroups
.ancestry
AS t0_r13, hostgroups
.vm_defaults
AS
t0_r14, hostgroups
.subnet_id
AS t0_r15, hostgroups
.domain_id
AS
t0_r16, hostgroups
.puppet_proxy_id
AS t0_r17, hosts
.id
AS t1_r0,
hosts
.name
AS t1_r1, hosts
.ip
AS t1_r2, hosts
.environment
AS
t1_r3, hosts
.last_compile
AS t1_r4, hosts
.last_freshcheck
AS t1_r5,
hosts
.last_report
AS t1_r6, hosts
.updated_at
AS t1_r7,
hosts
.source_file_id
AS t1_r8, hosts
.created_at
AS t1_r9,
hosts
.mac
AS t1_r10, hosts
.sp_mac
AS t1_r11, hosts
.sp_ip
AS
t1_r12, hosts
.sp_name
AS t1_r13, hosts
.root_pass
AS t1_r14,
hosts
.serial
AS t1_r15, hosts
.puppet_status
AS t1_r16,
hosts
.domain_id
AS t1_r17, hosts
.architecture_id
AS t1_r18,
hosts
.operatingsystem_id
AS t1_r19, hosts
.environment_id
AS t1_r20,
hosts
.subnet_id
AS t1_r21, hosts
.sp_subnet_id
AS t1_r22,
hosts
.ptable_id
AS t1_r23, hosts
.medium_id
AS t1_r24,
hosts
.build
AS t1_r25, hosts
.comment
AS t1_r26, hosts
.disk
AS
t1_r27, hosts
.installed_at
AS t1_r28, hosts
.model_id
AS t1_r29,
hosts
.hostgroup_id
AS t1_r30, hosts
.owner_id
AS t1_r31,
hosts
.owner_type
AS t1_r32, hosts
.enabled
AS t1_r33,
hosts
.puppet_ca_proxy_id
AS t1_r34, hosts
.managed
AS t1_r35,
hosts
.use_image
AS t1_r36, hosts
.image_file
AS t1_r37,
hosts
.uuid
AS t1_r38, hosts
.compute_resource_id
AS t1_r39,
hosts
.puppet_proxy_id
AS t1_r40, hosts
.certname
AS t1_r41,
hosts
.image_id
AS t1_r42 FROM hostgroups
LEFT OUTER JOIN hosts
ON
hosts
.hostgroup_id
= hostgroups
.id
WHERE ( (hostgroups.id in
(NULL))) AND ((hosts
.name
= BINARY ‘host1.tamu.edu’))
Completed 200 OK in 178ms (Views: 9.2ms | ActiveRecord: 141.7ms)host1