![Puppet:如何在 Puppet 3.0 模板中使用来自 MySQL 表的数据?](https://linux22.com/image/797183/Puppet%EF%BC%9A%E5%A6%82%E4%BD%95%E5%9C%A8%20Puppet%203.0%20%E6%A8%A1%E6%9D%BF%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%9D%A5%E8%87%AA%20MySQL%20%E8%A1%A8%E7%9A%84%E6%95%B0%E6%8D%AE%EF%BC%9F.png)
我有一些数据,其真实来源在 MySQL 数据库中,预计大小最大为几千行范围(在最坏的情况下),我想使用 puppet 在某些服务器上使用该数据配置文件(主要是在模板中遍历这些行)。
我目前正在使用 Puppet 3.0.x,我无法改变 MySQL 将成为该数据的权威来源这一事实。请注意,数据来自外部来源,而不是来自 puppet 或托管节点。
有哪些可行的方法?您会推荐哪一种?
外部节点分类器在这里有用吗?
我的“最后手段”是定期将表转储到 YAML 文件中,并通过 Hiera 将其读取到 Puppet 模板中,或者直接将表转储到一个或多个预格式化的文本文件中,以备复制到节点。
有一个SF 上未解答的问题关于系统用户,但根本问题可能与我的类似 - 他正试图从 MySQL 中获取数据。
答案1
我甚至更进一步,拥有一个权威的 MySQL 数据库全部服务器数据(实际上是一个 Django 应用程序)。下面是我整合它们的方法:
节点定义
有两种可能性。我们目前使用生成的清单,它包含在 site.pp 中,其中包含以下条目:
node "avrdb.prod.example.com" {
$sdb_status="live"
$sdb_db_type="slave"
$sdb_db_version="mysql_55"
$sdb_os="co56"
include "s_db::avrdb"
}
但很快我们需要将其切换到 ENC,因为 puppet 不再支持它。ENC 已经编写完毕,使用几乎相同的模板。
服务器数据
有些配方(例如我们的 DNS 主配方)需要有关服务器的额外数据。正如您所建议的那样,这是在模板中完成的。这是一个此类模板的示例:named.conf,它需要知道所有辅助名称服务器。它只是使用 mysql gem 来访问数据库。当然,数据库结构无关紧要,但完整的示例通常很好 :)
<%
require 'rubygems'
require 'mysql'
dbh = Mysql.real_connect('serverdb.prod.example.com', 'user', 'pass', 'serverdb')
int_slaves = dbh.query("SELECT ip_address, name FROM network_vip WHERE name LIKE 'idns%' ORDER BY name")
int_hosts = dbh.query("SELECT i.ip_address, a.name FROM servers_interface as i LEFT JOIN servers_asset as a ON i.asset_id=a.id WHERE a.name regexp '^idns-' and i.name='eth0'");
ext_slaves = dbh.query("SELECT ip_address, name FROM network_vip WHERE name LIKE 'edns%' ORDER BY name")
ext_hosts = dbh.query("SELECT i.ip_address, a.name FROM servers_interface as i LEFT JOIN servers_asset as a ON i.asset_id=a.id WHERE a.name regexp '^edns-*' and i.name='eth0'");
%>
options {
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
allow-recursion { none; };
allow-transfer { any; };
allow-query { any; };
// default for transfers-in and transfers-out is 10
transfers-in 128;
transfers-out 128;
// default for transfers-per-ns is 2
transfers-per-ns 8;
// serial-query-rate on a master has undocumented side-effects
// of notifying slaves much faster for many zones
serial-query-rate 4096;
// resolvers or authoritatives
also-notify {
// Internal slaves
<% int_slaves.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% }
int_slaves.data_seek(0) -%>
// Internal dns hosts
// Needed as we move all the boxes over to HA, where they
// query from eth0, not vip
<% int_hosts.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% }
int_hosts.data_seek(0) -%>
// External slaves
<% ext_slaves.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% } -%>
<% ext_hosts.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% }
ext_hosts.data_seek(0) -%>
};
};
include "/etc/rndc.key";
// logging
logging {
channel default_file { file "/var/log/named/named.log" versions 10 size 100m; print-time yes; print-category yes; };
channel security_file { file "/var/log/named/security.log" versions 10 size 100m; print-time yes; print-category yes; };
channel query_file { file "/var/log/named/query.log" versions 10 size 100m; print-time yes; print-category yes; };
channel debug_default { file "/var/log/named/debug.log" versions 10 size 100m; print-time yes; print-category yes; };
category general { default_file; default_debug; };
category security { security_file; };
category default { default_file; };
category queries { query_file; };
category edns-disabled { null; };
};
statistics-channels {
inet 127.0.0.1 port 8080;
};
// Do not put any zone declarations here unless they differ between views
// Put zones with data common to all views in commonzones.conf
// This view goes to all the internal nameservers
view "internal" {
match-clients {
// Internal slaves
<% int_slaves.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% } -%>
// Internal ds hosts
// Safety catch in case they request an AXFR on their eth0 ip
<% int_hosts.each{|slave| -%>
// <%= slave[1] %>
<%= slave[0] %>;
<% } -%>
};
zone "example.com" IN {
type master;
file "master/example.com-internal.zone";
};
// This path relative to chroot
include "/etc/commonzones.conf";
include "/etc/zones.rfc1918";
};
// This view goes to everyone else
view "others" {
match-clients { any; };
// Special zones that differ between views
zone "example.com" IN {
type master;
file "master/example.com-external.zone";
};
// This path relative to chroot
include "/etc/commonzones.conf";
include "/etc/zones.rfc1918";
};
答案2
您还可以使用hiera-mysql后端。如果您查看此后端的源代码,您会发现创建一个新的后端或自定义后端很容易。像这样使用 hiera 将使您的模板/清单代码更简洁,而不是使用 Dennis 建议的方法(也适用于 finel)