使用puppetlabs mysql模块

时间:2016-05-15 08:40:59

标签: puppet hiera

我有一个包含这个短语的木偶清单(我的问题是关于最后的生产块),用于设置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 '}'")并不是非常明显。

(“关键”可能是错误的词。请纠正我。我对傀儡有点新鲜。这本身可能就是为什么我无法通过谷歌搜索和阅读回答这个问题。)

感谢您的任何指示。

2 个答案:

答案 0 :(得分:1)

更新:我现在看到mysql模块提供了一个选项,可以将配置文件添加到开箱即用的配置文件中。以下是根据documentation

使用它的方法
$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值放在不同的hiera键下,则可以保存一些代码:

$override_options = {
  hiera('mysql_name') => hiera('web_mysql_server')
}

原始答案

如果您的实际意图只是创建配置文件,最好的方法是在templates中使用erb syntax

这是一个例子。首先,您将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'),
  }
}

答案 1 :(得分:0)

您使用web_mysql::mysql_server的方式表示该类的参数名为production。如果是这种情况,则在使用该类时必须使用该名称。相反,为什么不调用参数mysqlclient,因为它似乎是客户端配置参数?

回答原始问题:

你的清单版本都不是有效的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',
}