Checking for the existence of a file

808 views
Skip to first unread message

Ted Fiedler

unread,
Aug 7, 2013, 4:20:48 PM8/7/13
to puppet...@googlegroups.com
I would like to check for the existence of a template file and if that template exists, use it otherwise move on. The file would obviously need to exist on the Puppet server, not the client. Here is my code.

$filetest = File.exists?("/etc/puppetlabs/puppet/modules/samba/templates/$hostname.erb")  

                if $filetest == "true" {

                    file { '/etc/samba/smb.conf':
                       notify  => Service["smb", "winbind"],
                       replace => 'no',
                       path => '/etc/samba/smb.conf',
                       ensure => file,
                       content => template("samba/smb.erb", "samba/${hostname}.erb"),
                       require => Package["samba"],
                    }
                } else {

                    file { '/etc/samba/smb.conf':
                       notify  => Service["smb", "winbind"],
                       replace => 'no',
                       path => '/etc/samba/smb.conf',
                       ensure => file,
                       content => template("samba/smb.erb"),
                       require => Package["samba"],
                    }
                }

 
I keep getting the error:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '.'; expected '}' at /etc/puppetlabs/puppet/modules/samba/manifests/init.pp:34 on node


when I run the following test, it seems to work fine, but if I modify my code above within puppet to use this, it fails also...

# cat test.rb 
$test = File.exists?("/etc/puppetlabs/puppet/modules/samba/templates/file.erb") 
print $test
# ruby test.rb 
true

Any ideas?

A million thanks in advance

Ted

Ashley Penney

unread,
Aug 7, 2013, 4:47:58 PM8/7/13
to puppet...@googlegroups.com
You cannot use ruby code in Puppet directly, which is why this isn't working for you.

To do this you would need to write a "Puppet Function" as documented at http://docs.puppetlabs.com/guides/custom_functions.html.  This would then let you do something like:

if exists('/file') {

Functions always run on the master, not the agent, so this would then test for the file on the master.


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rilindo Foster

unread,
Aug 7, 2013, 5:01:19 PM8/7/13
to puppet...@googlegroups.com
This line here:

> content => template("samba/smb.erb", "samba/${hostname}.erb"),

What is your intention with having multiple .erb files per hostname? That might help us figure if there a different solution is warranted.

-Rilindo

Ted Fiedler

unread,
Aug 8, 2013, 9:30:12 AM8/8/13
to puppet...@googlegroups.com
I have a base Samba config and then about 50 server specific samba shares. So I apply a base config to all the servers, then if that server has extra shares that are not in the base, it is included. 
Reply all
Reply to author
Forward
0 new messages