Search code examples
puppet

adding two lines to a file using puppet


I'm trying to add two lines to the end of a file using puppet. I have configured my class with the following:

file { '/etc/services':
  ensure => present,
}->
file_line { 'Append a line to services':
  path => '/etc/services',
  line => 'service1        9200/tcp                        # service1',
}->
file_line { 'Append a line to services':
  path => '/etc/services',
  line => 'service2        9201/tcp                        # service2',
}

But I keep getting the error:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: File_line[Append a line to services] is already declared at (file: /path/to/template.pp, line: 1472); cannot redeclare (file: /path/to/template.pp, line: 1476) (file: /path/to/template.pp, line: 1476, column: 1) on node x

Have I missed something obvious?

n.b. The first line was added some time ago and works fine, I just want to add the second line now.


Solution

  • In Puppet DSL both the title and namevar of a resource must be unique. The title is also often aliased to the namevar. In the file_line resource the title is perfunctory, and therefore should merely be a description of the managed line in the file, but still must be unique.

    We can modify the titles to be descriptive:

    file { '/etc/services':
      ensure => present,
    }->
    file_line { 'Ensure TCP port 9200 in services':
      path => '/etc/services',
      line => 'service1        9200/tcp                        # service1',
    }->
    file_line { 'Ensure TCP port 9201 in services':
      path => '/etc/services',
      line => 'service2        9201/tcp                        # service2',
    }
    

    and this will solve your issue as the titles are now unique, and there is no more duplicate declaration.