0
votes

I would like to add a functionality in the Puppet manifest to apply one configuration if another file exists, if doesn't exists should apply a different configuration. I have tried this part of code:

    exec { "chk_file_exist":
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -f ${file} && echo 0",
    before => File['/etc/systemd/system/config1.service'],
   }

   exec { "chk_file_doesnt_exist":
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test ! -f ${file} && echo 0",
    refreshonly => true,
    before => File['/etc/systemd/system/config2.service'],
   }

   file { '/etc/systemd/system/config1.service':
       content => template('module/service/config1.service'),
       require => Exec["chk_file_exist"],
   }

   file { '/etc/systemd/system/config2.service':
       content => template('module/service/config2.service'),
       require => Exec["chk_file_doesnt_exist"],
      } 

It creates just one file, but it throws an error:

Error: Could not find command 'chk_file_exist'
Error: /Stage[main]/mymodule::service/Exec[chk_file_exist]/returns: change from 'notrun' to ['0'] failed: Could not find command 'chk_file_exist' (corrective)

How I can avoid this error?

Thank you

1
Conveniently, there are many answers on Stack Overflow for how to apply a file resource given the file's existence. Give it a search and you will find the assistance you are looking for. - Matt Schuchard
@MattSchuchard : Thanks for your reply. I have found many questions on StackOverflow related to this topic, but none solving my issue. Can you please provide an indication of how I can bypass this error? - dante
They should all explain how to write a custom fact to check for its existence, and then how to use it as a conditional in your Puppet code. That is what you need here. - Matt Schuchard
Any way around, Exec resources and resource relationships do not serve this objective. - John Bollinger

1 Answers

0
votes

You are getting that error because you haven't given the exec resources a command attribute. An exec resource defaults to using its title as a command.

Try something like the following, which uses true as a command that doesn't do anything.

exec { "chk_file_exist":
  command => 'true',
  path    =>  ["/usr/bin","/usr/sbin", "/bin"],
  onlyif  => "test -f ${file} && echo 0",
  before  => File['/etc/systemd/system/config1.service'],
}