给定一系列主机名,如何根据puppet中的主机名生成一组文件

时间:2015-04-24 15:45:38

标签: puppet

我不确定是否有办法在傀儡中做到这一点,但这就是我想做的事。

鉴于这个骨架木偶类定义......

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地址。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

如果名称属于由Puppet管理的节点,则可以使用puppetdbquery module直接从PuppetDB获取地址

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

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

答案 1 :(得分:0)

我找到了解决方案......

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

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

谢谢