Search code examples
puppet

How to use concat in puppet for combining two files into same file?


define nagios::iconf( $host_name='', $ip='', $short_alias='',$service_name='',$remote_host_name='',$port=''){
$reconfigure = "/usr/local/nagios/etc/import/${host_name}.cfg"

   concat{$reconfigure:
      owner => nagios,
      group => nagios,
      mode  => 755
   }

   concat::fragment{"hosttemplate":
      target => $reconfigure,
      source => template('nagios/host.erb'),
      order  => 01,
   }

   concat::fragment{"servicetemplate":
      target => $reconfigure,
      ensure  => template("nagios/${service_name}.erb"),
      order   => 15
   }
}
include nagios

When I declare in site.pp

node "blahblahhostname"{
nagios::iconf{'name1':
  host_name       => 'localhost'
  remote_host_name => 'blahblah1',
  ip      => '32.232.434.323',
  port    => '111',
  short_alias     => 'random',
  service_name    => 'servicename1'
}

nagios::iconf{'name2':
  host_name       => 'localhost'
  remote_host_name => 'blahblah1',
  ip      => '32.232.434.323',
  port    => '111',
  short_alias     => 'random',
  service_name    => 'servicename2'
}
include nagios
}

I get duplicate declaration error. Where did I go wrong?


Solution

  • Problem here is that

    concat{"/usr/local/nagios/etc/import/localhost.cfg"
      owner => nagios,
      group => nagios,
      mode  => 755
    }
    

    is defined twice because of the host_name. When you are calling the define type in your manifest it is causing the duplicate warnings.

    You have to define that just once, outside of the define type. May be in a class or in the manifests itself. Something like:

    concat{"/usr/local/nagios/etc/import/localhost.cfg"
    .....
    .....
    

    And then, rest of the code, i.e. concat::fragments can go inside the define type.

    define nagios::iconf( $host_name='', $ip='',     $short_alias='',$service_name='',$remote_host_name='',$port=''){
    $reconfigure = "/usr/local/nagios/etc/import/${host_name}.cfg"
       concat::fragment{"hosttemplate":
          target => $reconfigure,
          source => template('nagios/host.erb'),
          order  => 01,
       }
    
       concat::fragment{"servicetemplate":
          target => $reconfigure,
          ensure  => template("nagios/${service_name}.erb"),
          order   => 15
       }
    }