How to use foreman query in puppet manifest?

Problem: Can’t use foreman query in puppet manifest nodes section

Expected outcome:

Foreman and Proxy versions: 1.18.0

Foreman and Proxy plugin versions:

Other relevant data:
Hello everyone!

I have this manifest:

$foremanlogin = file(’/etc/puppetlabs/code/environments/production/manifests/foremanlogin.txt’)
$foremanpass = file(’/etc/puppetlabs/code/environments/production/manifests/foremanpass.txt’)

$query = foreman({foreman_user => “$foremanlogin”,
foreman_pass => “$foremanpass”,
item => ‘hosts’,
search => ‘hostgroup = “Web Servers”’,
filter_result => ‘name’,
})

$quoted = regsubst($query, ‘(.*)’, ‘"\1"’)
$query6 = join($quoted, “,”)

notify{“The value is: ${query6}”: }

node ${query6} {

package { ‘atop’:
ensure => ‘installed’,
}

}

When I execute this on agent I got error:

Server Error: Could not parse for environment production: Syntax error at ‘’

Error in my node block

node ${query6}

Now if I remove this from code:

node ${query6} {

package { ‘atop’:
ensure => ‘installed’,
}

}

I see correct output from notify, my variable looks like this:

“test-ubuntu1”,“test-ubuntu2”

Variable in correct node manifest format.

I don’t understand whats wrong? variable query6 is correct.

How to fix that?

A more common way is to use the ENC. Then you’d write a class (for example profile::webserver) where you write all the needed things for a webserver (like install package atop). You can then import classes into Foreman and add the class to the host group Web Servers.

Have you considered this option or is there a specific reason you want to use foreman()?

There’s also a blog post about this function: Foreman :: update - Getting Foreman search results into your Puppet manifest