Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is the official documentation - https://docs.puppet.com/facter/3.1/fact_overview.html#writing-structured-facts

Note: Structured facts are supported in Puppet 3.3 and greater, but they aren’t enabled by default. To enable structured facts, set the stringify_facts option to false in the [main] section of puppet.conf on all machines, whether they are agents, masters, or standalone nodes running puppet apply.

Puppet Enterprise 3.7 and later has structured facts enabled by default, as will Puppet 4.0.

I don't currently have structured facts enabled, but I think that all you have to do is the following.. Enable structured facts if appropriate (noted above) and return the object in the type you want from ruby, inside of your fact.. (hash or array)

Here's the link

Facter.add(:interfaces_array) do
  setcode do
   interfaces = Facter.value(:interfaces)
   # the 'interfaces' fact returns a single comma-delimited string, e.g., "lo0,eth0,eth1"
   # this splits the value into an array of interface names
   interfaces.split(',')
  end
end

Now, in your case if you have bash returning a set of newline separated values.... You would need to shell out to your command, and then turn the output into a newline separated array.

Facter.add(:ls_tmp_array) do
  setcode do
  ls_output = Facter::Core::Execution::exec('ls -l /tmp')
   ls_output.lines
  end
end

Here is the official documentation - https://docs.puppet.com/facter/3.1/fact_overview.html#writing-structured-facts

Note: Structured facts are supported in Puppet 3.3 and greater, but they aren’t enabled by default. To enable structured facts, set the stringify_facts option to false in the [main] section of puppet.conf on all machines, whether they are agents, masters, or standalone nodes running puppet apply.

Puppet Enterprise 3.7 and later has structured facts enabled by default, as will Puppet 4.0.

I don't currently have structured facts enabled, but I think that all you have to do is the following.. Enable structured facts if appropriate (noted above) and return the object in the type you want from ruby, inside of your fact.. (hash or array)

Here's the link

Facter.add(:interfaces_array) do
  setcode do
   interfaces = Facter.value(:interfaces)
   # the 'interfaces' fact returns a single comma-delimited string, e.g., "lo0,eth0,eth1"
   # this splits the value into an array of interface names
   interfaces.split(',')
  end
end

Now, in your case if you have bash returning a set of newline separated values.... You would need to shell out to your command, and then turn the output into a newline separated array.

Facter.add(:ls_tmp_array) do
  setcode do
  ls_output = Facter::Core::Execution::exec('ls -l /tmp')
   ls_output.lines
  end
end

One additional thing... as per this comment on stack overflow...

You may need to remove the newlines from each line in the array by using .map(&:squish)....

Facter.add(:ls_tmp_array) do
  setcode do
  ls_output = Facter::Core::Execution::exec('ls -l /tmp')
   ls_output.lines.map(&:squish)
  end
end

Here is the official documentation - https://docs.puppet.com/facter/3.1/fact_overview.html#writing-structured-facts

Note: Structured facts are supported in Puppet 3.3 and greater, but they aren’t enabled by default. To enable structured facts, set the stringify_facts option to false in the [main] section of puppet.conf on all machines, whether they are agents, masters, or standalone nodes running puppet apply.

Puppet Enterprise 3.7 and later has structured facts enabled by default, as will Puppet 4.0.

I don't currently have structured facts enabled, but I think that all you have to do is the following.. Enable structured facts if appropriate (noted above) and return the object in the type you want from ruby, inside of your fact.. (hash or array)

Here's the link

Facter.add(:interfaces_array) do
  setcode do
   interfaces = Facter.value(:interfaces)
   # the 'interfaces' fact returns a single comma-delimited string, e.g., "lo0,eth0,eth1"
   # this splits the value into an array of interface names
   interfaces.split(',')
  end
end

Now, in your case if you have bash returning a set of newline separated values.... You would need to shell out to your command, and then turn the output into a newline separated array.

Facter.add(:ls_tmp_array) do
  setcode do
  ls_output = Facter::Core::Execution::exec('ls -l /tmp')
   ls_output.lines
  end
end

One additional thing... as per this comment on stack overflow...

You may need to remove the newlines from each line in the array by array... The linked question suggest using .map(&:squish).... squish, but that didn't work for me, so I used the chomp method

Facter.add(:ls_tmp_array) do
  setcode do
  ls_output = Facter::Core::Execution::exec('ls -l /tmp')
   ls_output.lines.map(&:squish)
ls_output.lines.map(&:chomp)
  end
end