人偶清单脚本中的 Hiera 变量

Hiera variables inside a puppet manifest script

我有 Hiera 运行 和 puppet/vagrant 组合。

假设我有一个傀儡执行官正在执行以下操作

      exec { create-project-database:
    command => 'mysql -u root -e "CREATE DATABASE project_db DEFAULT CHARACTER SET = \'utf8\';"',
    unless  => 'mysql -u root information_schema -e "select * from information_schema.schemata;" | grep "project_db"',
    require => Package[mysql-server],
  }

我想用我的 common.yaml of hiera

中的变量替换字符串 "project_db"
---
machine_message: 'This is the dev machine!'
codes_path: '/vagrant/code/laravel/'
project_db: 'project_db'

我该怎么做?语法对我来说不清楚,像这样会带来错误:

    command => 'mysql -u root -e "CREATE DATABASE 'hiera('project_db)' DEFAULT CHARACTER SET = \'utf8\';"',

你在 'hiera('project_db)' 中的引号有问题(即你没有在括号前结束,所以 hiera('project_db') 应该是正确的)

通常我之前会从 hiera 声明变量,所以像这样

$DB_NAME = hiera('project_db')
exec { create-project-database:
  command => "mysql -u root -e \"CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET = 'utf8';\"",
  unless  => 'mysql -u root information_schema -e "select * from information_schema.schemata;" | grep "project_db"',
  require => Package[mysql-server],
}