Search code examples
puppet

Puppet in operator


I'm writing a conditional statement in Puppet (3.7) that tests whether the hostname is in a defined array of hostnames. If true, the class continues to run, if not, it exits with the fail function

if $::hostname in $approved_hosts != str2bool("true") {
  fail("This module is for approved reposync hosts only")

Where $approved_hosts is an array of hostnames. This method does not work, but if I change $approved_hosts to ['hostname1', 'hostname2'] it does work:

if $::hostname in ['hostname1', 'hostname2'] != str2bool("true") {
  fail("This module is for approved reposync hosts only")

Can anyone explain why when I convert the array of hostnames to a variable the condition fails but works otherwise?

Thanks :)


Solution

  • Adjust your code (fail to notify), did the test in a test.pp file, it runs fine.

      $approved_hosts = ['hostname1', 'hostname2']
      if $::hostname in $approved_hosts != str2bool("true")
       { notify {"This module is for approved reposync hosts only":} }
    

    So that means your code is no problem at array define, it should be something else.