Ask Your Question

"\n" not working in puppet

asked 2015-08-17 06:31:10 -0600

Chutki gravatar image


Below is manifests

$aaa = hiera('LINE')

  file { '/opt/test:
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => "${aaa}\n",

below is the rspec

let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
  hiera ={ :config => 'spec/fixtures/hiera/hiera.yaml' })

  tempa = hiera.lookup('LINE',nil,nil)

  let(:hiera_data) {{
    :LINE => tempa

    it{ should contain_file('/opt/test').with(
    :ensure  => 'file',
    :owner   => 'root',
    :group   => 'root',
    :mode    => '0644',
    :content => tempa,

getting below error

expected that the catalogue would contain File[/opt/test] with content set to "testfile" but it is set to "testfile\n"

should I remove "\n" from manifests.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-08-17 15:12:14 -0600

cbarbour gravatar image

Your syntax is correct, however your test needs to check for the newline character. Alternatively, you can test using a regex expression, such as /tempa/. This will match whether or not the newline character is present.

Beyond that, I recommend against spec testing with Hiera. Your module should not hard-code hiera lookups. Instead, use automatic parameter lookups. If you prefer to disable automatic parameter lookups, use the roles and profiles pattern, and put your Hiera queries in your profiles. If that isn't an option, parameterize the class and set your hiera query as the default parameter, with a default value in case the value isn't set in Hiera.

The idea of Hiera is that you can decouple your modules from your queries, creating a separation of domain logic and module logic. Embedding hiera queries in the module defeats that principle.

BTW, if you're on Puppet 4 and don't need to maintain Puppet 3 compatibility, consider using the lookup function rather than the hiera() function.

edit flag offensive delete link more


getting "expected that the catalogue would contain File[/opt/test] with content set to /tempa/ but it is set to "SSC\n"" error, when I use regular expression /tmpa/ option.

Chutki gravatar imageChutki ( 2015-08-18 07:13:59 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-08-17 06:31:10 -0600

Seen: 432 times

Last updated: Aug 17 '15