Use facter facts in puppet

sagar picture sagar · Nov 8, 2016 · Viewed 8.9k times · Source

I am new to puppet and planning to implement it in our environment.

I have puppet agents that are running on different versions of Redhat.

Now, I am planing to push repo files from the puppet master, and I need your guidance to implement the same.

I have the below manifests.

file { 'local_repo':
  ensure => file,
  path   => '/etc/yum.repos.d/local.repo',
  mode   => "600",
  source => 'puppet:///modules/repo/rhel7.1',
}

file { 'local_repo':
  ensure => file,
  path   => '/etc/yum.repos.d/local.repo',
  mode   => "600",
  source => 'puppet:///modules/repo/rhel6.7',
}

When I execute the Facter CLI I get the below output.

[root@dheera ~]# facter os
{
  architecture => "x86_64",
  family => "RedHat",
  hardware => "x86_64",
  name => "RedHat",
  release => {
    full => "7.2",
    major => "7",
    minor => "2"
  }
}

I want to make use of the above output and execute my manifests accordingly. That is, if the puppet agent is executing on Redhat 7.1, then the Puppet master uses the corresponding file.

Answer

Matt Schuchard picture Matt Schuchard · Nov 8, 2016

You can do this by using the Facter variable inside the source attribute and then interpolating it within the string. Note that your ' must be changed to " to interpolate the variable in the string.

Facter 2/Puppet 3:

file { 'local_repo':
  ensure => file,
  path   => '/etc/yum.repos.d/local.repo',
  mode   => "600",
  source => "puppet:///modules/repo/rhel${::os['release']['full']}",
}

Facter 3/Puppet 4:

file { 'local_repo':
  ensure => file,
  path   => '/etc/yum.repos.d/local.repo',
  mode   => "600",
  source => "puppet:///modules/repo/rhel${facts['os']['release']['full']}",
}

You can find helpful documentation here: https://docs.puppet.com/puppet/4.8/reference/lang_facts_and_builtin_vars.html

It is for the latest versions, but contains legacy Puppet/Facter information too.