2

This might be obvious. However, after searching through facter's help, puppetlab's website, and Google I am still unable to figure out how to retrieve a nested facter fact .

For example, I can do:

>facter os

{"release"=>{"major"=>"6", "minor"=>"7", "full"=>"6.7"}, "family"=>"RedHat", "name"=>"CentOS"}

How do I retrieve os['name'] or os['release']['minor'] or any arbitrary nested fact via command line with facter?

Swartz
  • 294
  • 5
  • 14

2 Answers2

3

Nested fact values can be viewed in the CLI by using a dot between variables

e.g. to retrieve os['release']['minor'] in the CLI type: facter os.release.minor

EDIT: Apperently this only works with facter 3.x.

This doc gives a brief mention on how to access these structured (aka nested) facts (http://docs.puppetlabs.com/facter/3.1/core_facts.html):

Legacy Facts Note: As of Facter 3, legacy facts such as architecture are hidden by default to reduce noise in Facter’s default command-line output. These older facts are now part of more useful structured facts; for example, architecture is now part of the os fact and accessible as os.architecture. You can still use these legacy facts in Puppet manifests ($architecture), request them on the command line (facter architecture), and view them alongside structured facts (facter --show-legacy).

Unfortunately I cannot find information about accessing nested facts using older versions.

In facter v3 you can do the following:

facter os
{
  architecture => "amd64",
  distro => {
    codename => "trusty",
    description => "Ubuntu 14.04.3 LTS",
    id => "Ubuntu",
    release => {
      full => "14.04",
      major => "14.04"
    }
  },
  family => "Debian",
  hardware => "x86_64",
  name => "Ubuntu",
  release => {
    full => "14.04",
    major => "14.04"
  },
  selinux => {
    enabled => false
  }
}

.

facter os.release
{
  full => "14.04",
  major => "14.04"
}

.

facter os.release.major
14.04
Martin K
  • 169
  • 6
1

That's not what facter is supposed to do. If you use it within puppet as expected, you can access every fact separately.

To a certain degree, you can work around this if you use JSON output:

facter --json os | grep major
      "major": "6",

and if you have a CLI JSON parser available, everything is possible.

Using jq:

facter --json os | jq .os.release.minor
   "6"
Sven
  • 97,248
  • 13
  • 177
  • 225