Friday, April 24, 2015

Generating files based on puppet array

First off a big thank you to alexharv074 and ask.puppetlabs.com for giving me this solution.

I need to generate the files:

# more /tmp/rabbit-* /tmp/mongo-el7-001.cfg 
::::::::::::::
/tmp/rabbit-1.cfg
::::::::::::::
define host {
         use                     linux-server
         host_name               rabbit-1
         alias                   rabbit-1
         hostgroups              rabbit_hosts 
         address                 10.29.103.33
}
::::::::::::::
/tmp/rabbit-2.cfg
::::::::::::::
define host {
         use                     linux-server
         host_name               rabbit-2
         alias                   rabbit-2
         hostgroups              rabbit_hosts 
         address                 10.29.103.34
}
::::::::::::::
/tmp/mongo-el7-001.cfg
::::::::::::::
define host {
         use                     linux-server
         host_name               mongo-el7-001
         alias                   mongo-el7-001
         hostgroups              mongo_hosts 
         address                 10.29.103.31

}

... so I wrote this puppet class:

class make_files (
     $rabbit_servers = ['rabbit-1:10.29.103.33','rabbit-2:10.29.103.34'],
     $mongo_servers = ['mongo-el7-001:10.29.103.31'],
) {
    define my_file ($content, $hostgroup) {
          $tuple = split($name, ':')
          $host_name = $tuple[0]
          $file_name = "/tmp/$host_name.cfg"
          $ipaddress = $tuple[1]
          $config = "define host {
              use                     linux-server
              host_name               $host_name
              alias                   $host_name
              hostgroups              $hostgroup 
              address                 $ipaddress
}"

        file { $file_name:
          ensure  => file,
          content => $config,
        }
    }
    $rabbit_content = join($rabbit_servers, ',')
    my_file { $rabbit_servers: content => $rabbit_content, hostgroup => 'rabbit_hosts' }
    $mongo_content = join($mongo_servers, ',')
    my_file { $mongo_servers: content => $mongo_content, hostgroup => 'mongo_hosts' }
}

No comments:

Post a Comment