首页 > 解决方案 > Cannot parse hash from hiera into erb template

问题描述

I have a puppet manifest that looks like this:

$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] ]

and hiera code:

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>

I am using an erb template that looks like this:

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

to try and produce a file that looks like this:

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

I have tried a variety of different approaches (this is simply the latest) which result in either an error from puppet:

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

or results in a file that looks like this:

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

I know the answer is somehow to do with how I am handling arrays and hashes and that the solution is staring me in the face, but I am currently clueless as to my next move.

标签: puppethiera

解决方案


我找到了解决方案。这主要是因为我的 yaml 文件有其他没有“设置”部分的 web 应用程序导致它崩溃。我的最终代码如下所示。

希拉:

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]

设置.conf.erb:

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

推荐阅读