给定一组主机名,我如何根据 puppet 中的这些主机名生成一组文件

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

我不确定是否有办法在 puppet 中做到这一点,但这就是我正在尝试做的。

鉴于此骨骼人偶 class 定义 ...

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

...生成文件...

# 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 
        }

其中IP地址为对应主机的IP地址。

非常感谢任何帮助。

我找到了解决方案...

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' }
}

...我发现我需要稍微更改文件的内容。 这可能不是最佳答案,但它似乎有效。我愿意接受建议的改进。

谢谢

如果名称属于由 Puppet 管理的节点,您可以使用 puppetdbquery module

直接从 PuppetDB 获取地址
$ipaddress = query_facts("hostname=$host_name", ['ipaddress'])['ipaddress']

代码未经测试,我不确定调用是否正确。