Custom Facts in Puppet

 

================== CUSTOM FACT =================

puppet@testserver:/opt/puppetserver/conf/environments/test/modules/helloworld/lib/facter> cat dhcp_dnsdomain.rb

    Facter.add(“dhcp_dnsdomain”) do
      confine :operatingsystem => ‘SLES’
      setcode do
        Facter::Core::Execution.exec(‘/usr/sbin/dhcpcd-test eth0 | grep DNSDOMAIN | cut -d$”\'” -f 2 | rev | cut -d”.” -f 1,2 | rev’)
      end
    end

==================== PUPPET MANIFEST ============

puppet@testserver:/opt/puppetserver/conf/environments/test/modules> cat helloworld/manifests/init.pp

class helloworld {
notify { “DNSDOMAIN value: $::dhcp_dnsdomain “: }

if ($::operatingsystem == ‘SLES’) and ($::virtual == ‘hyperv’) {
if $::dhcp_dnsdomain == ‘internal.cloudapp.net’ {
notify { “SLES hyperv VM $::hostname runs on Azure “: }
}
else {
notify { “SLES hyperv VM $::hostname DOES NOT run on Azure  “: }
}
}
else {
notify { “OS $::operatingsystem and hypervisor $::virtual combination not valid for dhcp_dnsdomain “: }
}
}

============== PUPPET AGENT OUTPUT ==============

 

Notice: DNSDOMAIN value: rwe.com
Notice: /Stage[last]/Helloworld/Notify[DNSDOMAIN value: rwe.com ]/message: defined ‘message’ as ‘DNSDOMAIN value: rwe.com ‘
Notice: SLES hyperv VM s01tl0101 DOES NOT run on Azure
Notice: /Stage[last]/Helloworld/Notify[SLES hyperv VM s01tl0101 DOES NOT run on Azure  ]/message: defined ‘message’ as ‘SLES hyperv VM s01tl0101 DOES NOT run on Azure  ‘
Notice: Finished catalog run in 1.29 seconds