resource file with option source or content depending if source does exist

121 views
Skip to first unread message

Andreas Dvorak

unread,
Oct 17, 2014, 9:05:39 AM10/17/14
to puppet...@googlegroups.com
Dear all,

I would like to use the file resource to supply a host specific file. If it does exist choose it and if not create a file from a template.
I tried this, but then /mnt/motd needs to be on all server.

  $a = file("/mnt/motd/motd-${::hostname}",'/dev/null')
  if($a != '') {
    file {$config_file:
      ensure => 'file',
      owner  => 'root',
      group  => 'root',
      mode   => '0644',
      source => "puppet:///motd_files/motd/motd-${::hostname}",
    }
  }
  else {
    file { $config_file:
      ensure  => 'file',
      owner   => 'root',
      group   => 'root',
      mode    => '0644',
      content => template($template),
    }
  }

If I try this
 $a = file("puppet:///motd_files/motd/motd-${::hostname}",'/dev/null')
I get the error that Files must be fully qualified

Does anybody has a solution for that?

Best regards
Andreas

Felix Frank

unread,
Oct 20, 2014, 7:07:46 AM10/20/14
to puppet...@googlegroups.com
On 10/17/2014 03:05 PM, Andreas Dvorak wrote:
> If I try this
> $a = file("puppet:///motd_files/motd/motd-${::hostname}",'/dev/null')
> I get the error that Files must be fully qualified
>
> Does anybody has a solution for that?

Hi,

so are you running masterless?

Andreas Dvorak

unread,
Oct 20, 2014, 8:29:40 AM10/20/14
to puppet...@googlegroups.com
Hi,

no I habe a puppet master.

Best regards,
Andreas

jcbollinger

unread,
Oct 20, 2014, 9:23:55 AM10/20/14
to puppet...@googlegroups.com


On Friday, October 17, 2014 8:05:39 AM UTC-5, Andreas Dvorak wrote:
Dear all,

I would like to use the file resource to supply a host specific file. If it does exist choose it and if not create a file from a template.
I tried this, but then /mnt/motd needs to be on all server.

  $a = file("/mnt/motd/motd-${::hostname}",'/dev/null')
  if($a != '') {
    file {$config_file:
      ensure => 'file',
      owner  => 'root',
      group  => 'root',
      mode   => '0644',
      source => "puppet:///motd_files/motd/motd-${::hostname}",
    }
  }
  else {
    file { $config_file:
      ensure  => 'file',
      owner   => 'root',
      group   => 'root',
      mode    => '0644',
      content => template($template),
    }
  }


No, in that case /mnt/motd/* has to be on the machine that builds the catalog, as that's where all Puppet functions will be evaluated.  Since you are using a puppetmaster for that purpose, that's where the files need to be.  I'd probably write it like this, though:

  $a = file("/mnt/motd/motd-${::hostname}",'/dev/null')
  file {$config_file:
    ensure => 'file',
    owner  => 'root',
    group  => 'root',
    mode   => '0644',
    content => $a ? { '' => template($template), default => $a }
  }


John

Andreas Dvorak

unread,
Oct 21, 2014, 7:02:09 AM10/21/14
to puppet...@googlegroups.com
Hi,

thank you, the line

content => $a ? { '' => template($template), default => $a }
solves my problem.

Best regards,
Andreas
Reply all
Reply to author
Forward
0 new messages