Red Cricket
Red Cricket

Reputation: 10470

Given an array of hostnames, how can I generate a set of files based on those hostnames in puppet

I am not sure there is a way to even do this in puppet, but here is what I am trying to do.

Given this skeletal puppet class definition ...

class make_files (
        $rabbit_servers = ['rabbit-1','rabbit-2'],
        $mongo_servers = ['mongo-1','mongo-2'],
) {
...
}

... generate the files ...

# pwd
/root/conf/hosts
# more rabbit-*
::::::::::::::
rabbit-1.cfg
::::::::::::::
define host {
        use                     linux-server
        host_name               rabbit-1
        alias                   Rabbit MQ host
        hostgroups              rabbit_hosts 
        address                 10.29.103.33 
        }
::::::::::::::
rabbit-2.cfg
::::::::::::::
define host {
        use                     linux-server
        host_name               rabbit-2
        alias                   Rabbit MQ host
        hostgroups              rabbit_hosts 
        address                 10.29.103.34 
        }
# more mongo-*
::::::::::::::
mongo-1.cfg
::::::::::::::
define host {
        use                     linux-server
        host_name               mongo-1
        alias                   Mongo DB host
        hostgroups              mongo_hosts 
        address                 10.29.103.31 
        }
::::::::::::::
mongo-2.cfg
::::::::::::::
define host {
        use                     linux-server
        host_name               mongo-2
        alias                   Mongo DB host
        hostgroups              mongo_hosts 
        address                 10.29.103.32 
        }

Where the IP addresses are the IP address of the corresponding host.

Any help is much appreciated.

Upvotes: 0

Views: 258

Answers (2)

Felix Frank
Felix Frank

Reputation: 8223

If the names belong to nodes that are managed by Puppet, you can fetch the addresses from PuppetDB directly using the puppetdbquery module

$ipaddress = query_facts("hostname=$host_name", ['ipaddress'])['ipaddress']

The code is untested, I'm not sure whether the invocation is correct.

Upvotes: 2

Red Cricket
Red Cricket

Reputation: 10470

I have found a solution ...

class make_files (
                $rabbit_servers = ['rabbit-1:10.29.103.33','rabbit-2:10.29.103.34'],
                $mongo_servers = ['ost-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' }
}

... and I found that I needed to change the content of the files slightly. This is probably not the best answer but it seems to work. I am open to suggested improvements.

Thanks

Upvotes: 0

Related Questions