Search code examples
regexpuppetinterpolationstring-interpolation

Concatenate strings in file_line resource


I'm trying to concatenate a string in a puppet manifest like so:

file_line {'Append to /etc/hosts':
  ensure => present,
  line =>  "${networking['ip'] + '\t'}${networking['fqdn'] + '\t'}${networking['hostname']}",
  match => "${'^#?'+ networking['ip'] + '\s+' + networking['fqdn'] + '\s+' + networking['hostname']}",
  path =>  '/etc/hosts'
}

I either get syntax errors or in the case above:

The value '' cannot be converted to Numeric

Which I'm guessing means that it doesn't like the plus operator. So how do I interpolate the strings in the match and line attributes?


Solution

  • The problem here is that the operator + is restricted to only Numeric types (documentation). It cannot be used with String types. However, the spacing and regular expressions can still be used as normal without attempting a string concatenate. These merely need to be placed outside the variable interpolation. Therefore:

    file_line { 'Append to /etc/hosts':
      ensure => present,
      line   => "${networking['ip']}\t${networking['fqdn']}\t${networking['hostname']}",
      match  => "^#?${networking['ip']}\s+${networking['fqdn']}\s+${networking['hostname']}",
      path   => '/etc/hosts'
    }
    

    should resolve your issues with the type mismatch and the + operator.