Ask Your Question

Creating custom facts: how to access structured facts Facter.value ?

asked 2015-09-29 17:44:57 -0600

kmizser gravatar image

Reading through Facter 3.1 docs there is a section talking about using other facts by calling Facter.value. Here is the exact section.

Here's the relevant line: distid = Facter.value(:lsbdistid)

My question is how to access a structured fact in Facter.value, for example if I wanted to use the 'ec2metadata->amiid' fact in that line instead of lsbdistid.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2015-09-30 16:35:21 -0600

GregLarkin gravatar image

I think you should be able to retrieve the complete fact value then extract the hash element that you want, e.g.:

ec2metadata = Facter.value(:ec2metadata)
ami_id = ec2metadata['ami_id']

I'm not a Ruby expert, and I haven't tested this, so YMMV, but please post back here if that solves your problem or not.

edit flag offensive delete link more

answered 2015-10-01 04:38:18 -0600

jorhett gravatar image

Facter.value will retrieve the top-level item and it will be the Ruby type appropriate for the data. If the fact contains a hash, then the item retrieved will be a Ruby Hash. If an Array or Boolean, then... in short, the Ruby methods appropriate for the data type will be available for you on the item retrieved.

As this is Ruby, I'd be surprised if this doesn't work:

ami_id = Facter.value(:ec2metadata)['ami-id']
edit flag offensive delete link more

answered 2015-10-12 14:00:28 -0600

kmizser gravatar image

Thanks GregLarkin and jorhett for the answers, it works both ways, except that I didn't need the colon (:) in front of ec2_metadata.

edit flag offensive delete link more

answered 2015-10-22 07:24:23 -0600

Gibbsoft gravatar image

From the CLI you can use dot-notation to address the keys in the hash, for example:

facter ec2_metadata.ami-id
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-09-29 17:43:06 -0600

Seen: 3,584 times

Last updated: Oct 22 '15