> ## Content Index
> Fetch the complete content index at: https://kudithipudi.org/llms.txt
> Use this file to discover other available public pages before exploring further.

# HOW TO : Use templates in puppet to pass hostnames
- URL: https://kudithipudi.org/how-to-use-templates-in-puppet-to-pass-hostnames/
- Published: 2012-04-27T03:00:46.000Z
- Updated: 2012-04-27T03:00:46.000Z
- Description: puppet, is a configuration management framework that can be used to perform several different things to validate/configure your infrastructure. We have been using puppet for sometime...
- Author: admin
- Tags: HOWTO, Linux, Technology, Uncategorized, #wp, #wp-post, #Import 2026-08-23 02:32

[puppet](http://puppetlabs.com/?ref=kudithipudi.org), is a configuration management framework that can be used to perform several different things to validate/configure your infrastructure. We have been using puppet for sometime at my work and have just started moving into some of the advanced uses of the tool.

One of the features offered by puppet is the capability to use templates to configure different servers.

For example, say you want to configure an application on server ABCD, XYZ and 123\. And the configuration file for all these servers is the same, other than the hostname of the server. The configuration file has to reside in /opt/application/config.conf . The config.xml file looks like this

```

db.name=blah
db.user=blahblah
db.hostname=XYZ
log.level=ERROR
log.location=/var/log/application

```

Here is how you can do it in puppet.

Define a module which uses a template and then configure the template to put the host specific entry in the template. Let’s name our module test\_config

- Create the module
  - cd $PUPPET\_HOME/modules
  - mkdir test\_config/{files,manifests,templates}
- Create the template
  - cd templates
  - vi config.conf.template and add the following to the file  
  ```  
  db.name=blah  
  db.user=blahblah  
  db.hostname=<%= fqdn %>  
  log.level=ERROR  
  log.location=/var/log/application  
  ```
    - note : see how I replaced the hostname XYZ, which was specific to one server with <%= fqdn %>. This is one of the “facts” provided by puppet. you can get a list of all the facts by running facter on any of the puppet clients.
- Configure the module to use the template. In this case, we want the module to place the file config.conf in /opt/application
  - cd manifests
  - vi init.pp and add the following to the file  
  ```  
  class test_config {  
  file { "/opt/application/config.conf":  
  ensure => present,  
  owner => appuser,  
  group => appuser,  
  mode => 755,  
  content => template("test_config/config.conf.template"),  
  }  
  }  
  ```
    - note : There are several other options you can use for the class file.. I just gave an example of some of the common ones. Like setting the owner, group and the rights.
- Finally configure the clients to use the module. In the individual node config files, include the module you just created. Here is how the config for node ABCD would look like  
```  
node ABCD {  
include test_config  
}  
```

The next time the puppet client runs on host ABCD, it would create the file /opt/application/config.conf with the right hostname in the config file.