无法将hiera中的哈希解析为erb模板

时间:2018-05-27 12:03:33

标签: puppet hiera

我有一个看起来像这样的木偶清单:

$webapp_list = lookup('webapps', Hash, 'hash')
$webapp_list.each | $app_name, $app_info| {
  $app_port = $app_info[port]
  $app_description = $app_info[description]
  $app_settings = [ $app_info[settings] ]

和hiera代码:

webapps:
    webapp-template:
            port: 5001
            description: "Webapp Description"
            live: false
            settings:
                    FLASK_APP: appname.py
                    MAIL_USERNAME: email@example.com
                    MAIL_PASSWORD: <extra strong password>

我使用的erb模板如下所示:

<% @app_settings.each do |settingKey, settingValue| -%>
  export <%= settingKey -%>='<%= settingValue -%>'
<% end %>

尝试生成一个如下所示的文件:

export FLASK_APP=appname.py
export MAIL_USERNAME=email@example.com
export MAIL_PASSWORD=<extra strong password>

我尝试了各种不同的方法(这只是最新的方法),导致木偶出错:

Info: Using configured environment 'special'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, Failed to parse template webapps/settings.conf.erb:
  Filepath: /etc/puppet/code/modules/webapps/templates/settings.conf.erb
  Line: 2
  Detail: undefined method `each' for nil:NilClass
 at /etc/puppet/code/modules/webapps/manifests/init.pp:40:18 on node web.home
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

或导致文件如下所示:

export {"FLASK_APP"=>"appname.py", "MAIL_USERNAME"=>"email@example.com", "MAIL_PASSWORD"=>"<extra strong password>"}=''

我知道答案在某种程度上与我如何处理数组和哈希并且解决方案正在盯着我看,但我目前对于我的下一步行动一无所知。

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。这主要是因为我的yaml文件我有其他webapps没有&#34;设置&#34;导致它倒下的部分。我的最终代码可以在下面看到。

Hiera:

webapps:
    webapp-template:
        port: 5001
        description: "Webapp Description"
        live: false
        settings:
            FLASK_APP: appname.py
            MAIL_USERNAME: email@example.com
            MAIL_PASSWORD: <extra strong password>

木偶清单:

$webapp_list = lookup('webapps', Hash, 'hash')
$webapp_list.each | $app_name, $app_info| {

  $app_port = $app_info[port]
  $app_description = $app_info[description]
  $app_settings = $app_info[settings]

Settings.conf.erb:

<% @app_settings.each do |key,value| -%>
  export <%= key %>='<%= value %>'
<% end -%>