Ask Your Question
0

Howto handle facter lists?

asked 2014-05-14 11:15:46 -0500

upietz gravatar image

updated 2014-05-14 11:16:21 -0500

Hi,

I'm looking for a way to parse a fact that returns a list (@interfaces). I need to create a file for each interface reported, but my initial idea causes a syntax error:

$file_prefix = "/tmp/interface_"
@interfaces.each |$iface| {
  file { $file_prefix + $iface:
    ensure => present,
  }
}

@interfaces is e.g.

# facter -p interfaces
lo0,em0

The error message is rather generic:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '}' at </path/to/>manifests/test.pp:7:1 on node <node>
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-05-14 14:59:48 -0500

updated 2014-05-14 15:09:06 -0500

First of all , It looks like you haven't set parser to future.

Run this command:

puppet config print parser

If it returns current, you don't have access to the .each function. To change this, edit /etc/puppet/puppet.conf, and put parser = future under the [main] block. The above command should then return future.

Second , the return is a string and not an array so it will try use each letter not each interface.

So, you need to split it first before use each.

Third and last :D , you can't use + to concatenate string , if you want to concatenate string put both together between double quotes.

So , after enable parser = future in your puppet.conf , you can use a code like this:

$file_prefix = "/tmp/interface_"
$ifaces = split($::interfaces, ',')
$ifaces.each |$iface| {
file { "${file_prefix}${iface}":
  ensure => present,
  }
}

OUTPUT: when I run this code with noop.

Notice: Compiled catalog for renanvicente.localhost in environment production in 1.44 seconds
Notice: /Stage[main]/Main/File[/tmp/interface_eth1]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]/Main/File[/tmp/interface_lo]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]/Main/File[/tmp/interface_eth0]/ensure: current_value absent, should be present (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 3 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.49 seconds
edit flag offensive delete link more

Comments

Thanks a lot. We're using the current parser for reasons I have to investigate ;-) I'm still learning that, though they're very alike, puppet isn't all ruby- and I have a lot to learn on that too. But that solves my issue.

upietz gravatar imageupietz ( 2014-05-16 06:25:47 -0500 )edit

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: 2014-05-14 11:15:46 -0500

Seen: 160 times

Last updated: May 14 '14