官术网_书友最值得收藏!

Handling multiline with HEREDOC

Writing multiline file fragments in Puppet mostly resulted in code that was hard to read, mostly due to indentation. With Puppet 4, the heredoc style was added. It is now possible to specify a heredoc tag and marker:

$motd_content = @(EOF)
  This system is managed by Puppet
  local changes will be overwritten by next Puppet run.
EOF

The heredoc tag starts with an @ sign followed by arbitrary string enclosed in parenthesis. The heredoc marker is the string given in the tag.

If variables are required inside the heredoc document, the variable interpolation can be enabled by putting the tag string in double quotes. Variables inside the heredoc are written like Puppet DSL variables: a dollar sign followed by the scope and the variable name:

$motd_content = @("EOF")
  Welcome to ${::fqdn}.
  This system is managed by Puppet version ${::puppetversion}.
  Local changes will be overwritten by the next Puppet run
EOF

Normally, heredoc does not handle escape sequences. Escape sequences need to be enabled explicitly. As of Puppet 4.2, heredoc has the following escape sequences available:

  • \n Newline
  • \r Carriage return
  • \t Tab
  • \s Space
  • \$ Literal dollar sign (preventing interpolation)
  • \u Unicode character
  • \L Nothing (ignore line breaks in source code)

Enabled escape sequences have to be placed behind the string of the heredoc tag:

$modt_content = @("EOF"/tn)
Welcome to ${::fqdn}.\n\tThis system is managed by Puppet version ${::puppetversion}.\n\tLocal changes will be overwritten on next Puppet run.
EOF

In the example, the text always starts in the first column, which makes it hard to read and stands out from the code around it, which will usually be indented by some amount of whitespace.

It is possible to strip indentation by placing whitespaces and a pipe sign in front of the heredoc marker. The pipe sign will indicate the first character of each line:

$motd_content = @("EOF")
    Welcome to ${::fqdn}.
    This system is managed by Puppet version ${::puppetversion}.
    Local changes will be overwritten on next Puppet run.
    | EOF

Now heredoc and inline_epp can be easily combined:

class my_motd (
  Optional[String] $additional_content = undef
){
  $motd_content = @(EOF)
    Welcome to <%= $::fqdn %>.
    This system is managed by Puppet version <%= $::puppetversion %>.
    Local changes will be overwritten on next Puppet run.
    <% if $additional_content != undef { -%>
    <%= $additional_content %>
    <% } -%>
    | EOF
  file { '/etc/motd':
    ensure  => file,
    content => inline_epp($motd_content, { additional_content => $additional_content } ),
  }
}

Declaring this class will give the following result in the motd file:

puppet apply -e 'include my_motd'
Welcome to puppetmaster.example.net.
This system is managed by Puppet version 4.2.1.
Local changes will be overwritten on next Puppet run.

Note

When using heredoc in combination with inline_epp, you want to take care to not quote the heredoc start tag. Otherwise, the variable substitution will take place prior to the inline_epp function call.

主站蜘蛛池模板: 衡阳县| 和顺县| 将乐县| 西畴县| 丰原市| 上犹县| 义马市| 怀化市| 海林市| 吉木乃县| 锦屏县| 卢氏县| 宁波市| 屏东县| 茶陵县| 茌平县| 宜春市| 土默特右旗| 胶州市| 灵宝市| 浮山县| 南丹县| 曲阳县| 巴林左旗| 敖汉旗| 岑巩县| 宜兴市| 盐津县| 济南市| 平南县| 元朗区| 余江县| 天气| 安徽省| 潼南县| 聂荣县| 衢州市| 湘潭县| 方山县| 库伦旗| 民和|