multiple similar files on one host from one template in puppet

231 Views Asked by At

all. This seems like something other people would have already answered, but I couldn't find it, so apologies if this is an old question.

I want to make multiple files on one host from the same template. These are dnsmasq hosts files for IPs on an internal vs external network. One file needs the internal IP addrs, and one file needs the external ip addrs.

If that doesn't make sense, imagine I want to put a form letter in each user's home dir. The letter might say "Hi <%= @username %>".

I can't do something like

$username = 'bob'
file { '/home/bob/HELLO':
  content => template('hello/HELLO.erb')
}
$username = 'fred'
file { '/home/fred/HELLO':
  content => template('hello/HELLO.erb')
}

How do I do that?

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

You code will not work because you cannot reassign variables in puppet.

Use define to create your own parametrized type. E.g:

define myfile ($username, $path)
{
    file { $path:
        content => template('hello/HELLO.erb')
    }
}

Next use it:

myfile {'file1': 
   $username => 'bob', 
   $path     => '/home/bob/HELLO',
} 

myfile {'file2': 
   $username => 'fred', 
   $path     => '/home/fred/HELLO',
}