2

I am creating a manifest to get an Agent's partition name.

The fact $partitions shows the detail of the partition info.

{"xvda1"=>{"uuid"=>"d1697425-49d0-4c9f-9901-5f9260be8196", "size"=>"83859300", "mount"=>"/", "label"=>"cloudimg-rootfs", "filesystem"=>"ext4"}}

But, I just want to get the name part (xvda1) and use it as a variable for a configuration file.

Is there any way to filter the output in Puppet?

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
Mike
  • 297
  • 5
  • 17

1 Answers1

4

The fastest way to solve this would be to use the keys function from puppetlabs/stdlib: https://forge.puppet.com/puppetlabs/stdlib/readme.

keys() Returns the keys of a hash as an array. Type: rvalue.

With that function, we can transform the output hash from Facter into an array of the keys and access its elements normally. Assuming that xvda1 is the 0th element,

Facter 2:

$variable = keys($::partitions)[0]

Facter 3:

$variable = keys($facts['partitions'])[0]

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • I tried it but got an error Facter 2 : "Error while evaluating a Function Call, keys(): Requires hash to work with at" Facter 3 : "Error: Operator '[]' is not applicable to an Undef Value" – Mike Jan 11 '17 at 20:59
  • @Mike Those imply the fact was not looked up correctly. Is `$partitions` the name of the fact you wanted? Did you make a typo on the fact name? – Matthew Schuchard Jan 11 '17 at 22:46
  • Yes the $partitions is the fact I want $hddname = keys($::partitions)[0] file {'puppet_facts_example': path => '/tmp/test.txt', content => " HDDname is $hddname",} I got a hash error. – Mike Jan 12 '17 at 03:02
  • I found the solution for hash issue from http://stackoverflow.com/questions/40330166/unable-to-use-facts-as-a-hash-in-the-puppet-manifest?rq=1 Thanks Matt – Mike Jan 12 '17 at 03:09
  • @Mike Your problem was you were using a too-old version of Puppet? That was how I solved the problem in that question. – Matthew Schuchard Jan 12 '17 at 12:30
  • So, @Mike, the problem was with `stringify_facts`? In that event, your question is misleading: if `stringify_facts` is enabled then the fact value was not what you presented in the first place. – John Bollinger Jan 12 '17 at 14:12
  • @JohnBollinger I used version 3.8.1 and the default stringify_facts was true. After I changed it to false, everything was ok. Finally I upgraded the master to 4.8.1 and stringify_facts is false by default in 4.8.1 – Mike Jan 13 '17 at 14:13
  • @MattSchuchard yes I used version 3.8.1 and I had to change the stringify_facts to false to make your suggestion ( $variable = keys($::partitions)[0]) work. Finally I upgrade the master to 4.8.1. – Mike Jan 13 '17 at 14:16