Ask Your Question
0

edit custom facter results

asked 2017-09-10 04:04:44 -0500

sinaowolabi gravatar image

Hi

I have a custom fact that returns a series of values (its returning the names of subdirectories in a directory):

"check_subdirs":" three two one..",

I need to be able to edit the values to pass them as part of a python regex inside a manifest file.

one_* two_* three_*

please how do I do this to each value in the string?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2017-09-10 20:08:00 -0500

Provided your fact is structured like in my answer to your accompanying question, you can use the map function to transform your fact.

$_regexp_elements = $facts['datadirs'].map |$x| {
   "${x}_.*"
}

$_regexp_concatenated = $_regexp_elements.reduce |$m, $v| {
  "${m} ${v}"
}

[This goes into your Puppet code (your *.pp files). That's what I understood by your statement “[…] inside a manifest file”.]

edit flag offensive delete link more

Comments

1

I concur, the above example is a useful way to externally 'edit the values' of a fact, within the manifest. The below ruby solution is something that I would use in puppet 3.x, where I don't have access to better core puppet language

DarylW gravatar imageDarylW ( 2017-09-11 08:17:07 -0500 )edit
0

answered 2017-09-10 15:08:26 -0500

sinaowolabi gravatar image

updated 2017-09-10 15:55:25 -0500

I kind of got it to work by googling ruby:

Facter.add(:datadirs) do
    dirs = []
    setcode do
            if File.directory? '/data/files'
                    result = Facter::Core::Execution.exec('/bin/ls /data/files/ | sed "s/_//g" | sed "s/-//g" | /bin/tr  "\n" " " | sed "s/ *$//"')
            end
            dirs << result.split(/ /)
            dirs.flatten!
            stringdirs = dirs.map { |x|  "#{x}_.*" }
            stringdirs.to_s.gsub('"', '')
            stringdirs = stringdirs * " "
            end

end

This returns:

"two_.*" "one_.*"

and likewise on the target puppet client hosts.

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

1 follower

Stats

Asked: 2017-09-10 04:04:44 -0500

Seen: 175 times

Last updated: Sep 10