Saturday, August 19, 2017

puppet :: How to copy a file only if an account exists

I scoured the internet to see if anyone had already done something like this. I couldn't believe that I found nothing. The only bits I found was for, if another file exists or if a directory exists.

Anyway, this is how I did it.

First step I did was to create a custom fact. I named the custom fact isoracle. Then, in my class, I checked to see if isoracle true or false. If isoracle true, I copied the file.

The custom fact goes in this file:
./modules/files4all/lib/facter/isoracle.rb

The contents of the file is:
Facter.add(:isoracle) do
  setcode do
    isoracle = Facter::Util::Resolution.exec('id oracle 2>/dev/null')
    if isoracle == ""
      isoracle = false
    else
      isoracle = true
    end
  end
end

The class file goes in this file:
./modules/files4all/manifests/oracle.pp

The contents of this file is:
class files4all::oracle {

#Check the custom fact to see if oracle account exists
#skip if oracle account does not exists
  if $isoracle == true {
#These are the defaults unless overridden
    File {
      ensure => "present",
      owner  => "oracle",
      group  => "oracle",
      mode   => "0644",
    }

    file {
      [ "/oracle", "/oracle/sbin" ]:
      mode   => "0755",
      ensure => "directory",
    }

    file { "/oracle/testfile":
      source => "puppet:///modules/files4all/oracle/testfile",
    }

  }

}

What will happen is, puppet will first copy the isoracle.rb file to the client and run it. From there, puppet will be able to determine if the oracle account exists or not. I suppose you could modify the code to check for anything you want.

No comments:

Post a Comment