我有一个木偶清单,其中包含这个短语(我的问题是最后的生产块),用于设置mysql配置文件:
class web_mysql_server {
include web_mysql::packages
include web_mysql::mysql_server
}
class web_mysql::packages {
Package { ensure => installed }
package { 'libmysqlclient-dev': }
}
class web_mysql::mysql_server {
# Qualified keys (dot notation: web_mysql_server.mysql_database) isn't available
# until hiera 2.0.
$mysql_config = hiera('web_mysql_server')
# https://forge.puppetlabs.com/puppetlabs/mysql
class { '::mysql::server':
root_password => '...',
remove_default_accounts => true,
override_options => {
# The data in this block gets written to /etc/mysql/my.cnf.
'mysqld' => {
max_connections => '1024', # No good reason but parroting others.
key_buffer_size => '512M', # No good reason but parroting others.
},
'production' => {
adaptor => 'mysql2',
database => $mysql_config['mysql_database'],
user => $mysql_config['mysql_username'],
password => $mysql_config['mysql_password'],
host => '0.0.0.0',
encoding => 'UTF8',
},
但有时我可能希望数据库是一个临时数据库,因此将其称为生产数据库有点令人困惑。没问题,我最近了解了hiera,它是我今天所有钉子的锤子:
$mysql_config['mysql_name'] => {
adaptor => 'mysql2',
database => $mysql_config['mysql_database'],
user => $mysql_config['mysql_username'],
password => $mysql_config['mysql_password'],
host => '0.0.0.0',
encoding => 'UTF8',
},
不,这不起作用,尽管错误("syntax error at line (...with '=>'...), expected '}'"
)并没有太大的揭示性。
("钥匙"可能是个错误的词。请纠正我。我对木偶有点陌生。这可能就是我无法通过谷歌搜索和阅读来回答这个问题的原因。)
谢谢你的指点。
更新:我现在看到mysql
模块提供了一个选项,可以直接将部分添加到配置文件中。以下是根据文档应该如何使用它:
$override_options = {
$mysql_config['mysql_name'] => {
adaptor => 'mysql2',
database => $mysql_config['mysql_database']
# ...
}
}
class { '::mysql::server':
root_password => 'strongpassword',
remove_default_accounts => true,
override_options => $override_options,
}
或者,如果将mysql_name
值放在不同的层次键下,也可以保存一些代码:
$override_options = {
hiera('mysql_name') => hiera('web_mysql_server')
}
原始答案:
如果您的实际意图只是创建一个配置文件,那么最好的方法是使用erb语法中的模板。
下面是一个例子。首先,将my.cnf.erb
文件放入模块的templates
目录(例如modules/web_mysql/templates
):
[<%= @mysql_config['mysql_name'] %>]
adaptor = mysql2
database = <%= @mysql_config['mysql_database'] %>
user = <%= @mysql_config['mysql_username'] %>
password = <%= @mysql_config['mysql_password'] %>
host = 0.0.0.0
encoding = UTF8
您可以在web_mysql::mysql_server
类中使用此模板来创建配置文件:
class web_mysql::mysql_server {
# ...
$mysql_config = hiera('web_mysql_server')
file { '/etc/mysql/my.cnf':
ensure => 'present',
content => template('web_mysql/my.cnf.erb'),
}
}
使用web_mysql::mysql_server
的方式表明该类有一个名为production
的参数。如果是这种情况,那么在使用类时必须使用该名称。相反,既然它似乎是客户端配置参数,为什么不直接调用参数mysql
或client
呢?
原问题答案:
你的清单的两个版本都不是有效的Puppet。例如,把这个放在我的清单中:
'production' => {
adaptor => 'mysql2',
}
并且运行puppet apply --noop --modulepath modules manifests/host.pp
导致:
Error: Could not parse for environment production: Syntax error at '=>' at [...]/host.pp:1:14 on node hostname
你是想说这样的话吗?
$database_configuration = {
adaptor => 'mysql2',
database => $mysql_config['mysql_database'],
user => $mysql_config['mysql_username'],
password => $mysql_config['mysql_password'],
host => '0.0.0.0',
encoding => 'UTF8',
}