Puppet querying Foreman

I'm trying to get a list of hostnames to populate an array from the Foreman
function. We have servers that are dedicated to clients and
non-production. We use naming {clientid}-{server_type}. So for example,
test1-search001, test1-search002, test2-search001, test2-search003,
client1-search001, client1-search002.

With my code below, I'm able to get back what appears to be json with the
following error. Line 111 is the discovery.zen.ping.unicast.hosts line.

$search_hostnames = "${clientid}-search"

$f = {
item => 'fact_values',
search => "${search_hostnames}",
filter_results => 'fact = hostname',
per_page => '20',
foreman_user => "${puppet_user}",
foreman_pass => "${puppet_pass}"
}

$hostlist = foreman($f)

file { '/root/search.txt':
ensure => present,
content => "${hostlist}",
}

elasticsearch::instance { $hostname:
config => {
'node.name' => $hostname,
'network.host' => '0.0.0.0',
'path.data' => '/opt/elastic/search',
'discovery.zen.ping.unicast.hosts' => ${hostlist},
},
}

Notice: Local environment: 'production' doesn't match server specified node
environment 'test', switching agent to 'test'.
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 500 on SERVER:
Server Error: Syntax error at '}' at
/etc/puppetlabs/code/modules/fm_search/manifests/init.pp:111:56 on node
test-search001.useast.mydomain.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

{total => 0, subtotal => 4, page => 1, per_page => 20, search =>
test-search, sort => {by => , order => }, results =>
{test-search001.useast.mydomain.com => {networking::hostname =>
test-search001, hostname => test-search001, networking::fqdn =>
test-search001.useast.mydomain.com, clientcert =>
test-search001.useast.mydomain.com, fqdn =>
test-search001.useast.mydomain.com}, test-search002.useast.mydomain.com =>
{hostname => test-search002, networking::hostname => test-search002,
clientcert => test-search002.useast.mydomain.com, fqdn =>
test-search002.useast.mydomain.com, networking::fqdn =>
test-search002.useast.mydomain.com}, test-search003.useast.mydomain.com =>
{hostname => test-search003, networking::hostname => test-search003, fqdn
=> test-search003.useast.mydomain.com, networking::fqdn =>
test-search003.useast.mydomain.com, clientcert =>
test-search003.useast.mydomain.com}, test-search004.useast.mydomain.com =>
{hostname => test-search004, networking::hostname => test-search004, fqdn
=> test-search004.useast.mydomain.com, clientcert =>
test-search004.useast.mydomain.com, networking::fqdn =>
test-search004.useast.mydomain.com}}}

After a lot of trial and error here's what I came up with. It may not be
elegant, but it works. It will pull in all hosts that match on the
${environment} in this case "test", so it pulled in test-search001,
test-search002, test-search003 & test-search004.

#Custom function searchparse
module Puppet::Parser::Functions
newfunction(:searchparse, :type => :rvalue) do |args|
oldarray = args[0]
newarray = Array.new
pos = 0
oldarray.each do |value|
newarray.push(value['name'])
pos = pos + 1
end
return newarray
end
end

#Final puppet code
$cluster_name = "${environment}-search"
class { 'java':
package => 'java-1.8.0-openjdk',
}

package { 'java-1.7.0-openjdk':
ensure => absent,
require => Class['java'],
}

package { 'jq':
ensure => present,
}

$f = {
item => 'hosts',
search => "${cluster_name}",
filter_results => 'fqdn',
per_page => '20',
foreman_user => "${puppet_user}",
foreman_pass => "${puppet_pass}"
}
$hostlist = foreman($f)
$hostlist2 = $hostlist['results']
$hostlist3 = searchparse($hostlist2)

#from this site: https://github.com/elastic/puppet-elasticsearch/issues/647
file { '/usr/local/bin/checklicense.sh':
ensure => present,
owner => 'root',
group => 'root',
mode => '0755',
source => 'puppet:///modules/fm_search/checklicense.sh',
require => Package['jq'],
}

file { '/etc/elasticsearch/license.json':
ensure => present,
content => "${elastic_license}",
require => Class['elasticsearch'],
}

class { 'elasticsearch':
repo_version => '5.x',
manage_repo => true,
java_install => false,
jvm_options => [
"-Xms${xms}",
"-Xmx${xmx}"
],
config => { 'cluster.name' => $cluster_name }
}

$elastic_config = {
'node.name' => $hostname,
'network.host' => '0.0.0.0',
'discovery.zen.ping.unicast.hosts' => $hostlist3,
'xpack.security.enabled' => 'false',
'discovery.zen.minimum_master_nodes' => '2',
'bootstrap.memory_lock' => 'true',
}

elasticsearch::instance { $hostname:
config => $elastic_config
}

Elasticsearch::Plugin { instances => $hostname }
elasticsearch::plugin { 'x-pack': }

class { 'kibana':
config => {
'server.host' => '0.0.0.0',
},
require => Class['elasticsearch'],
}

kibana_plugin { 'x-pack': }

exec { 'install license':
command => 'curl -XPUT http://localhost:9200/_xpack/license -d
@/etc/elasticsearch/license
.json ; sleep 5; /etc/init.d/kibana restart',
onlyif => '/etc/elasticsearch/checklicense.sh',
provider => 'shell',
require =>
[File['/etc/elasticsearch/license.json'],File['/usr/local/bin/checklicense.sh']
,Class['elasticsearch'],Class['kibana']]
}

Matt

ยทยทยท On Tuesday, August 8, 2017 at 10:13:44 AM UTC-4, Matt Shields wrote: > > I'm trying to get a list of hostnames to populate an array from the > Foreman function. We have servers that are dedicated to clients and > non-production. We use naming {clientid}-{server_type}. So for example, > test1-search001, test1-search002, test2-search001, test2-search003, > client1-search001, client1-search002. > > With my code below, I'm able to get back what appears to be json with the > following error. Line 111 is the discovery.zen.ping.unicast.hosts line. > > $search_hostnames = "${clientid}-search" > > $f = { > item => 'fact_values', > search => "${search_hostnames}", > filter_results => 'fact = hostname', > per_page => '20', > foreman_user => "${puppet_user}", > foreman_pass => "${puppet_pass}" > } > > $hostlist = foreman($f) > > file { '/root/search.txt': > ensure => present, > content => "${hostlist}", > } > > elasticsearch::instance { $hostname: > config => { > 'node.name' => $hostname, > 'network.host' => '0.0.0.0', > 'path.data' => '/opt/elastic/search', > 'discovery.zen.ping.unicast.hosts' => ${hostlist}, > }, > } > > Notice: Local environment: 'production' doesn't match server specified > node environment 'test', switching agent to 'test'. > Info: Retrieving pluginfacts > Info: Retrieving plugin > Info: Loading facts > Error: Could not retrieve catalog from remote server: Error 500 on SERVER: > Server Error: Syntax error at '}' at > /etc/puppetlabs/code/modules/fm_search/manifests/init.pp:111:56 on node > test-search001.useast.mydomain.com > Warning: Not using cache on failed catalog > Error: Could not retrieve catalog; skipping run > > > {total => 0, subtotal => 4, page => 1, per_page => 20, search => > test-search, sort => {by => , order => }, results => { > test-search001.useast.mydomain.com => {networking::hostname => > test-search001, hostname => test-search001, networking::fqdn => > test-search001.useast.mydomain.com, clientcert => > test-search001.useast.mydomain.com, fqdn => > test-search001.useast.mydomain.com}, test-search002.useast.mydomain.com > => {hostname => test-search002, networking::hostname => test-search002, > clientcert => test-search002.useast.mydomain.com, fqdn => > test-search002.useast.mydomain.com, networking::fqdn => > test-search002.useast.mydomain.com}, test-search003.useast.mydomain.com > => {hostname => test-search003, networking::hostname => test-search003, > fqdn => test-search003.useast.mydomain.com, networking::fqdn => > test-search003.useast.mydomain.com, clientcert => > test-search003.useast.mydomain.com}, test-search004.useast.mydomain.com > => {hostname => test-search004, networking::hostname => test-search004, > fqdn => test-search004.useast.mydomain.com, clientcert => > test-search004.useast.mydomain.com, networking::fqdn => > test-search004.useast.mydomain.com}}} >